Spring Security OAuth2 和 FormLogin 在一个应用程序中
Posted
技术标签:
【中文标题】Spring Security OAuth2 和 FormLogin 在一个应用程序中【英文标题】:Spring Security OAuth2 and FormLogin in a one application 【发布时间】:2015-11-16 03:51:00 【问题描述】:在我的 Spring Boot 应用程序中,我有用于管理的 RESTful API 和 MVC Web 仪表板。
是否可以在一个应用程序中同时拥有 - 用于 RESTful API 的 Spring Security OAuth2 身份验证/授权(基于令牌,无状态)和用于 Spring MVC Web 仪表板的 FormLogin(有状态)?
如何使用 Spring Boot 正确配置它?
【问题讨论】:
你可以在同一个应用中拥有多个 Spring Security 配置。 你能解决这个问题吗?谢谢 【参考方案1】:您需要为基于表单的登录和资源服务器安全表单 REST 端点配置 Web 安全
这是一个使用单点登录和单独部署的授权服务器的工作配置。
@Configuration
@EnableOAuth2Sso
@EnableWebSecurity
protected static class ResourceConfiguration extends WebSecurityConfigurerAdapter
@Value("$sso.url")
private String ssoUrl;
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
protected TokenStore tokenStore()
return new RedisTokenStore(redisConnectionFactory);
@Bean
@Primary
protected ResourceServerTokenServices tokenServices()
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
return defaultTokenServices;
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception
OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager();
authenticationManager.setTokenServices(tokenServices());
return authenticationManager;
@Override
protected void configure(HttpSecurity http) throws Exception
http.requestMatchers()
.and().authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers(HttpMethod.GET, "/static/**").permitAll()
.antMatchers(HttpMethod.GET, "/profile/**").permitAll()
.antMatchers(HttpMethod.GET, "/services/**").permitAll()
.anyRequest().authenticated()
.and().logout()
.invalidateHttpSession(true)
.logoutSuccessUrl(ssoUrl+"/logout")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.deleteCookies("JSESSIONID").invalidateHttpSession(true)
.permitAll();
@Configuration
@EnableResourceServer
@Order(1)
protected static class ResourceServerConfig extends ResourceServerConfigurerAdapter
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception
resources.resourceId("resource-id");
@Override
public void configure(HttpSecurity http) throws Exception
http.requestMatcher(new OAuthRequestedMatcher())
.authorizeRequests().anyRequest().fullyAuthenticated();
private static class OAuthRequestedMatcher implements RequestMatcher
public boolean matches(HttpServletRequest request)
String auth = request.getHeader("Authorization");
boolean haveOauth2Token = (auth != null) && auth.startsWith("Bearer");
boolean haveAccessToken = request.getParameter("access_token")!=null;
return haveOauth2Token || haveAccessToken;
【讨论】:
您的客户端应用程序是在 thymeleaf 中完成的?在资源服务器中有一个注销 url 很奇怪 @rebert trudel,不,认为不是。资源服务器安全配置只有这个``@Override public void configure(HttpSecurity http) throws Exception http.requestMatcher(new OAuthRequestedMatcher()) .authorizeRequests().anyRequest().fullyAuthenticated(); ``以上是关于Spring Security OAuth2 和 FormLogin 在一个应用程序中的主要内容,如果未能解决你的问题,请参考以下文章
使用 spring-session 和 spring-cloud-security 时,OAuth2ClientContext (spring-security-oauth2) 不会保留在 Redis
针对授权标头的Spring Security OAuth2 CORS问题
Spring Security 解析 —— Spring Security Oauth2 源码解析
带有 Jersey 和 Spring Security OAuth2 的 Spring Boot
Spring Cloud Security[微服务安全](一)初识Spring Cloud Security和OAuth2.0