具有适当角色的Spring安全性Access Denied事件

时间:2017-03-17 08:54:35

标签: java spring spring-mvc spring-security

大家好日子。

我有一个带弹簧安全层的spring mvc应用程序。 所以我配置安全性以允许所有具有四种角色之一的用户访问/ good-transfer / ** URL

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf()
                .disable()
                // For all static resources we got permissions
                .authorizeRequests()
                .antMatchers("/css/**", "/js/**", "/img/**","/font/**").permitAll()
                // Good transfer
                .antMatchers("/good_transfer/**").hasAnyRole(UserRole.ROLE_ASSEMBLER.name(),UserRole.ROLE_LOADER.name(),UserRole.ROLE_SHIPPER.name(),UserRole.ROLE_SELLER.name())
                .anyRequest().authenticated()
                .and();

        http.formLogin()
                // указываем страницу с формой логина
                .loginPage("/login")
                // указываем action с формы логина
                .loginProcessingUrl("/j_spring_security_check")
                // указываем URL при неудачном логине
                .failureUrl("/login?error")
                // Указываем параметры логина и пароля с формы логина
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                // даем доступ к форме логина всем
                .permitAll();

        http.logout()
                // разрешаем делать логаут всем
                .permitAll()
                // указываем URL логаута
                .logoutUrl("/logout")
                // указываем URL при удачном логауте
                .logoutSuccessUrl("/")
                // делаем не валидной текущую сессию
                .invalidateHttpSession(true);
    }

我使用角色为ROLE_ASSEMBLER的用户的登录名和密码提交登录表单,登录成功后我将落入/ controller:

@Controller
public class LoginController extends CommonController
{
    @RequestMapping("/login")
    public ModelAndView login()
    {
        return model("login");
    }

    @RequestMapping("/")
    public ModelAndView main(HttpServletRequest request)
    {
        String redirect = "login";

        if(request.isUserInRole(UserRole.ROLE_ASSEMBLER.name())||request.isUserInRole(UserRole.ROLE_LOADER.name())||request.isUserInRole(UserRole.ROLE_SHIPPER.name())||request.isUserInRole(UserRole.ROLE_SELLER.name()))
        {
            redirect = "good_transfer";
        }
        return new ModelAndView("redirect:/"+redirect);
    }

}

表达式request.isUserInRole(UserRole.ROLE_ASSEMBLER.name())返回true,所以我需要一个角色。之后,控制器将我重定向到/良好传输URL(我有一个该URL的控制器):

@Controller
public class GoodTransferController extends CommonController
{
    @RequestMapping("/good_transfer")
    public ModelAndView getAssemblyList()
    {
        ModelAndView model = model("good-transfer");
        return model;
    }
}

但我可以t each the controller方法:(。 它会抛出AccessDenied异常......

我不明白为什么。 ROLE_ASSEMBLER用户必须允许使用此URL。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

终于搞定了。

我的安全配置需要没有“ROLE_”前缀的用户角色。

将其更改为:

http.csrf()
                .disable()
                // For all static resources we got permissions
                .authorizeRequests()
                .antMatchers("/css/**", "/js/**", "/img/**","/font/**").permitAll()
                // Good transfer
                .antMatchers("/good_transfer/**").hasAnyRole(UserRole.ROLE_ASSEMBLER.role(),UserRole.ROLE_LOADER.role(),UserRole.ROLE_SHIPPER.role(),UserRole.ROLE_SELLER.role())
                .anyRequest().authenticated()
                .and();

它开始起作用了!