Cen*_*321 7 authentication spring oauth spring-security spring-security-oauth2
我正在编写一个具有链接到数据库的身份验证的小应用程序,此身份验证将由Oauth2方面管理(由@EnableAuthorizationServer和@EnableResourceServer注释的类).管理页面在同一个应用程序中有另一个身份验证,它将链接到另一个不同的数据库,并将使用正常的基于表单的身份验证.
我为此特定目的编写了以下Web安全配置类:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig{
@Configuration
@Order(5)
public static class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/admin_logout"))
.invalidateHttpSession(true).logoutSuccessUrl("/admin/login.html");
http.authorizeRequests()
.antMatchers("/admin/login.html").permitAll().antMatchers("/admin/protected.html")
.hasRole("ADMIN")
.and().formLogin().loginPage("/admin/login.html")
.loginProcessingUrl("/admin_login").defaultSuccessUrl("/admin/protected.html");
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
//Data source for form based auth
auth.inMemoryAuthentication().withUser("adminuser").password("adminpassword").roles("ADMIN");
}
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//Data source for Oauth
auth.inMemoryAuthentication().withUser("myuser").password("mypassword").roles("USER").and().withUser("test")
.password("testpassword").roles("USER");
}
}
Run Code Online (Sandbox Code Playgroud)
其他相关组件是:
授权服务器配置:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter{
@Autowired
AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager).tokenEnhancer(tokenEnhancer())
.tokenStore(tokenStore());
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("client")
.secret("secret")
.authorizedGrantTypes("password", "refresh_token")
.scopes("read", "write")
.resourceIds("resource").accessTokenValiditySeconds(60);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception
{
oauthServer.checkTokenAccess("isAuthenticated()");
}
@Bean
public TokenEnhancer tokenEnhancer() {
return new CustomTokenEnhancer();
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
}
Run Code Online (Sandbox Code Playgroud)
资源服务器配置:
@Configuration
@EnableResourceServer
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER-1)
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter{
@Autowired
TokenStore tokenStore;
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId("resource").tokenStore(tokenStore);
}
@Override
public void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/api/**").authenticated();
}
}
Run Code Online (Sandbox Code Playgroud)
你也可以在这里查看代码:https://github.com/cenobyte321/spring-oauth2-tokenenhancer-test/tree/webspeciallogin(Branch:webspeciallogin)
问题是AdminSecurityConfig类中的所有内容都被忽略,我可以进入protected.html页面而不用自己登录,并且不会创建指定的登录和注销处理URL.
另一方面,基于Oauth2的登录工作没有问题.我还没想出如何在Oauth2中指定一个AuthenticationManagerBuilder,大多数在线资源建议使用由Oauth适当读取的configureGlobal注入方法,这就是为什么它在上面的代码中设置的原因.
如何在单个启用Oauth2的应用程序中相互独立配置两个身份验证源?
问候.
你需要两件事:
AdminSecurityConfig优先级高于您的ResourceServerConfiguration. 虽然@EnableResourceServer注释的文档说它将WebSecurityConfigurerAdapter用硬编码的阶数 3 注册 a,但实际上它被ResourceServerOrderProcessor-10 的阶数覆盖。因此请确保您的AdminSecurityConfig订单低于 -10。确保将请求匹配器的配置限制HttpSecurity为AdminSecurityConfig与管理服务器关联的 URL,如下所示:
http.requestMatchers().antMatchers("/admin/**", "/admin_login", "/admin_logout")
.and()
.authorizeRequests()
.antMatchers("/admin/protected.html").hasRole("ADMIN")
.antMatchers("/admin/login.html").permitAll()
.and()
.formLogin().loginPage("/admin/login.html")
.loginProcessingUrl("/admin_login")
.defaultSuccessUrl("/admin/protected.html")
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/admin_logout"))
.invalidateHttpSession(true).logoutSuccessUrl("/admin/login.html")
;
Run Code Online (Sandbox Code Playgroud)
请注意嵌入代码的第一行http.requestMatchers().antMatchers("/admin/**", "/admin_login", "/admin_logout")。
请参阅 Dave Syer(Spring Security 作者之一)对类似问题的回答以供参考。
我针对 github 上的示例项目提出了包含上述修复的拉取请求。
| 归档时间: |
|
| 查看次数: |
1320 次 |
| 最近记录: |