在同一应用程序中的Spring Security Form日志记录和Outh2
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在同一应用程序中的Spring Security Form日志记录和Outh2,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3928字,纯文字阅读大概需要6分钟。
内容图文
![在同一应用程序中的Spring Security Form日志记录和Outh2](/upload/InfoBanner/zyjiaocheng/695/e604e9d353e346899a13d722af94b6b3.jpg)
我编写了一个示例spring应用程序,其中有一些使用spring-security-oauth2保护的REST服务.现在,我想将这些服务移至使用Spring安全表单登录的原始应用程序.
在原始应用程序中,我希望通过表单登录来保护rest服务免受spring-security-oauth2的侵害,并保护其他spring控制器.我想知道的是,这种方法是对还是错?如果是对的,我该如何完成此操作.
这是示例应用代码,使用ouath2,
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService userDetailsService; // Is this really needed?
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
@Configuration
public class OAuth2ServerConfiguration {
private static final String RESOURCE_ID = "restservice";
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
// @formatter:off
resources.resourceId(RESOURCE_ID);
// @formatter:on
}
@Override
public void configure(HttpSecurity http) throws Exception {
// http.authorizeRequests().antMatchers("/test").not().permitAll();
http.authorizeRequests().antMatchers("/test").authenticated();
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
private TokenStore tokenStore = new InMemoryTokenStore();
@Autowired
// @Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
// @formatter:off
endpoints.tokenStore(this.tokenStore).authenticationManager(this.authenticationManager);
// @formatter:on
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// @formatter:off
clients.inMemory().withClient("clientapp").authorizedGrantTypes("password", "refresh_token").authorities("USER")
.scopes("read", "write").resourceIds(RESOURCE_ID).secret("123456");
// @formatter:on
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setSupportRefreshToken(true);
tokenServices.setTokenStore(this.tokenStore);
return tokenServices;
}
}
}
这是原始应用程序配置的一部分.
@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MyStaysureSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder.inMemoryAuthentication().withUser("mycompany").password("mypsswd").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/rest/*", "/api-docs/**").permitAll().antMatchers("/**").authenticated().and().formLogin().defaultSuccessUrl("/dashboard").and().csrf().disable();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
解决方法:
Spring Security建立在过滤器链的有序列表上,对于每个请求,第一个具有匹配路径的请求将处理身份验证.您的组合应用程序中有3个过滤器链,一个由@EnableAuthorizationServer创建(默认顺序= 0),一个由@EnableResourceServer创建(默认顺序= 3)和由MyStaysureSecurityConfiguration创建(也带有order = 0).不允许有2个具有相同顺序的过滤器,因此您需要重新排列它们,并为它们提供适合您的用例的请求匹配器.也许您还是不需要@EnableAuthorizationServer(问题尚不清楚)?无论如何,这都很简单-您有2种选择(大约):
>从MyStaysureSecurityConfiguration中的请求匹配器中排除oauth2资源,并允许它们由资源服务器过滤器处理.
>将资源服务器过滤器重新排序为较低的顺序,并为其提供仅与oauth2资源匹配的请求匹配器.
内容总结
以上是互联网集市为您收集整理的在同一应用程序中的Spring Security Form日志记录和Outh2全部内容,希望文章能够帮你解决在同一应用程序中的Spring Security Form日志记录和Outh2所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。