我正在尝试保护我的spring应用程序,它具有不同的用户角色。虽然设置了身份验证部分,并且工作正常,但在实现授权部分的过程中,我意识到SecurityConfiguration extends WebSecurityConfigurerAdapter class中的两个覆盖方法之一的某些注释被忽略了。
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private WebApplicationContext applicationContext;
private CredentialsService userDetailsService;
@Autowired
private DataSource dataSource;
@PostConstruct
public void completeSetup() {
userDetailsService = applicationContext.getBean(CredentialsService.class);
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login")
.permitAll()
.and()
.formLogin()
.permitAll()
.and()
.httpBasic()
.disable()
.authorizeRequests()
.antMatchers("/admin", "/admin/**")
.hasRole("ADMIN")
.and()
.authorizeRequests()
.antMatchers("/employee", "/employee/**")
.hasRole("EMPLOYEE")
.and()
.authorizeRequests()
.antMatchers("/customer", "/customer/**")
.hasRole("CUSTOMER");
}
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(encoder())
.and()
.authenticationProvider(authenticationProvider())
.jdbcAuthentication()
.dataSource(dataSource);
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(encoder());
return authProvider;
}
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder(12);
}
}现在的问题如下所示,这个类对我的用户进行身份验证,但有一个主要缺点:
configure(final HttpSecurity http) throws Exception {完全被忽略了。
另一方面,如果我在类的顶部添加@Configuration注释,
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {被完全忽略,因此将破坏授权,因为它将无法在我的自定义UserDetailsService实现上调用getUsername()和getPassword。
如您所见,我使用了一个DaoAuthenticationProvider实例作为authenticationProvider,因为我的应用程序从外部数据库检索用户/密码。
我现在采用的快速修复方法是在我的主类上添加以下方法
@EnableGlobalMethodSecurity(
securedEnabled = true,
jsr250Enabled = true,
prePostEnabled = true
)以及在我的受限控制器上使用@Secured注释。这是可行的,但我想了解为什么Spring有如此奇怪的行为,以及我可以采取什么步骤来解决这些问题。
发布于 2021-03-02 19:34:15
由于要将角色分配给用户,因此请使用以下语法
.antMatchers("/admin", "/admin/**")
.hasRole("ADMIN")或
.antMatchers("/admin", "/admin/**")
.hasAuthority("ROLE_ADMIN")角色只是存储为带有"ROLE_“前缀的权限。
因此,角色"ADMIN“相当于权限"ROLE_ADMIN”。
编辑1
您还可以简化您的配置,以清楚地了解所有内容的来源。
由于您的UserDetailsService (CredentialsService)已经是一个bean,Spring Security将自动拾取它。
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
// The password encoder should be a bean
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder(12);
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login")
.permitAll()
.and()
.formLogin()
.permitAll()
.and()
.authorizeRequests()
.antMatchers("/admin", "/admin/**")
.hasRole("ADMIN")
.and()
.authorizeRequests()
.antMatchers("/manager", "/manager/**")
.hasRole("MANAGER")
.and()
.authorizeRequests()
.antMatchers("/customer", "/customer/**")
.hasRole("CUSTOMER");
}
}https://stackoverflow.com/questions/66431422
复制相似问题