2017-03-17 1 views
0

Guten Tag an alle.Frühling Sicherheit Zugriff verweigert Ereignis mit der richtigen Rolle

Ich bekam eine Federmvc-Anwendung mit Federsicherheitsschicht. So konfigurierte i Sicherheitszugriff zu ermöglichen, für die/gut-Transfer/** URL für alle Benutzer, die eine von vier Rollen haben

@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); 
    } 

I Login-Formular mit Login und Passwort des Benutzers mit Rolle ROLE_ASSEMBLER und nach einem erfolgreichen einreichen anmelden i/Controller fallen:

@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); 
    } 

} 

Der Ausdruck request.isUserInRole (UserRole.ROLE_ASSEMBLER.name()) gibt true zurück, so habe ich eine Rolle benötigt. Danach Controller mich/gut-Transfer URL umleitet (bekam ich einen Controller für die URL):.

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

aber ich kann t each the controller s Methode :( Es führt die AccessDenied Ausnahme ...

ich kann nicht verstehen, warum. Diese URL muss für ROLE_ASSEMBLER Benutzer zugelassen werden.

Bitte helfen sie mir.

Antwort

0

Schließlich bekam es funktionieren.

Meine Sicherheitskonfiguration wollte Benutzerrollen ohne Präfix "ROLE_".

Changed es an:

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

Und es begann zu arbeiten!

Verwandte Themen