Java 具有适当角色的Spring安全访问被拒绝事件
大家好 我得到了一个带有spring安全层的spring mvc应用程序。 因此,我将安全性配置为允许具有四个角色之一的所有用户访问/good transfer/**URLJava 具有适当角色的Spring安全访问被拒绝事件,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,大家好 我得到了一个带有spring安全层的spring mvc应用程序。 因此,我将安全性配置为允许具有四个角色之一的所有用户访问/good transfer/**URL @Override protected void configure(HttpSecurity http) throws Exception { http.csrf() .disable() // For all static resou
@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);
}
我提交了带有用户登录名和密码的登录表,用户的角色为_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\u ASSEMBLER.name())返回true,因此我得到了所需的角色。在该控制器将我重定向到/good-transfer-URL之后(我为该URL获得了一个控制器):
但是我可以t每个控制器s方法:(。
它抛出AccessDenied异常
我不明白为什么。这个URL必须允许角色\汇编程序用户使用
请帮帮我。终于成功了
我的安全配置需要不带“ROLE_u”前缀的用户角色
将其更改为:
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();
它开始工作了
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();