Spring安全表单登录和outh2在同一个应用程序中
我编写了一个示例spring应用程序,它使用Spring安全表单登录和outh2在同一个应用程序中,spring,spring-security,spring-security-oauth2,Spring,Spring Security,Spring Security Oauth2,我编写了一个示例spring应用程序,它使用spring-security-oauth2保护一些rest服务。现在我想将这些服务移动到使用spring安全表单登录的原始应用程序 在最初的应用程序中,我希望rest服务受到spring-security-oauth2的保护,并使用表单登录来保护其他spring控制器。我想知道的是,这个方法是对的还是错的,如果对的话,我怎么才能完成这个动作 这是示例应用程序代码,它使用ouath2 @Configuration @EnableWebSecurity
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();
}
}
这是原始应用程序配置的一部分
@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安全性构建在过滤器链的有序列表上,对于每个请求,第一个具有匹配路径的请求处理身份验证。您的组合应用程序中有3个筛选链,一个由
@EnableAuthorizationServer
创建(默认顺序为0),一个由@EnableResourceServer
创建(默认顺序为3),另一个由您的MyStaysureSecurityConfiguration
创建(同样顺序为0)。您不允许有两个顺序相同的过滤器,因此您需要重新排列它们,并为它们提供符合您的用例的请求匹配器。也许你根本不需要@EnableAuthorizationServer
(问题不清楚)?在任何情况下都非常简单-您有2个选择(大致):
MyStaysureSecurityConfiguration
中的请求匹配器中排除oauth2资源,并允许它们由资源服务器筛选器处理@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();
}
}