2017-03-17 86 views
0

美好的一天每個人。彈簧安全訪問拒絕具有適當角色的活動

我有一個彈簧mvc應用程序與彈簧安全層。 所以我配置的安全允許訪問的/良好的傳輸/ **網址誰擁有所有用戶的四個角色

@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 
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())返回真,所以我就需要一個作用。之後控制器重定向我/好轉移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 S分析:( 它拋出異常存取遭拒......

我不能老是明白爲什麼。這個URL必須允許ROLE_ASSEMBLER用戶。

請幫幫我。

回答

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(); 

,並開始工作了!