【发布时间】:2018-03-24 11:10:24
【问题描述】:
我使用带有 spring security 的 spring boot 2。
我将安全性拆分为 rest 和 mvc。
@EnableWebSecurity
public class MultiHttpSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Configuration
@Order(1)
public class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/rest/**")
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf().disable();
}
}
@Configuration
@Order(2)
public class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/img/**", "/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll().successHandler(new CustomAuthenticationSuccessHandler())
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessHandler(new CustomLogoutHandler())
.and().csrf().disable();
}
}
}
我在 db 中的角色是
超级用户、管理员、集成商。
在我的一个休息控制器中,我放了
@Secured("hasRole('user')")
我的应用程序中不存在此角色。
我尝试使用具有以下角色的用户:超级用户和集成商,并且成功了...
同样的事情
@PreAuthorize("hasAuthority('user')")
还有其他配置吗?
【问题讨论】:
标签: spring-boot spring-security