繁体   English   中英

如何根据具有spring安全性的角色限制对html页面的访问?

[英]How to restrict access to html pages based on roles with spring security?

我希望用户可以根据他们的角色访问html页面。 例如,只有具有HR角色的人员才能查看settings.html页面,只有管理员才能看到pendingrequest.html。

这是我的安全配置:

    @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_前缀添加到安全检查中定义的所有角色。

您可以重命名角色名称以使用ROLE_前缀或从配置中删除该前缀( 如何使用JavaConfig从Spring Security中删除ROLE_前缀?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM