Java 如何使用spring security限制基于角色的html页面访问?
我希望用户能够根据自己的角色访问html页面。例如,只有具有HR角色的人才能查看settings.html页面,只有经理才能查看pendingrequest.html 这是我的安全配置:Java 如何使用spring security限制基于角色的html页面访问?,java,html,spring,spring-security,roles,Java,Html,Spring,Spring Security,Roles,我希望用户能够根据自己的角色访问html页面。例如,只有具有HR角色的人才能查看settings.html页面,只有经理才能查看pendingrequest.html 这是我的安全配置: @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) pu
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DatabaseAuthenticationProvider authenticationProvider;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**", "/img/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().loginPage("/login").failureUrl("/login").defaultSuccessUrl("/")
.and().logout().logoutSuccessUrl("/login")
.and().authorizeRequests().antMatchers("/login").permitAll()
.antMatchers("/settings.html").hasRole("HR")
.antMatchers("/pendingRequests.html").hasRole("MANAGER")
.antMatchers("/settings.html","/pendingRequests.html").hasRole("ADMIN")
.anyRequest().authenticated().and().csrf().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider).eraseCredentials(false);
}
}
由于某种原因,我在那里尝试的方法不起作用,无论我扮演什么角色,我都看不到这些页面。Spring安全性默认为安全检查中定义的所有角色添加
role\uu
前缀
您可以重命名角色名称,使其具有
role\u
前缀,或从配置中删除该前缀()默认情况下,Spring security会将role\u
前缀添加到安全检查中定义的所有角色
您可以将角色名称重命名为具有role\uu
前缀,或从配置中删除该前缀()