【问题标题】:Not able to recognize user ROLE when redirecting page using Spring Security使用 Spring Security 重定向页面时无法识别用户角色
【发布时间】:2018-01-24 11:16:34
【问题描述】:

我正在使用 Spring security 和 Thymeleaf 开发我的项目。我有基本的 Spring Security 集成。

SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private DataSource dataSource;

    @Autowired
      public void configureGlobal (AuthenticationManagerBuilder auth) throws Exception
      {
        auth
          .jdbcAuthentication()
            .dataSource(dataSource);
      }

    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
              .antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/success", true)
                .and()
            .httpBasic();
      } 
}

SecurityWebApplicationInitializer.java

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer
{
    public SecurityWebApplicationInitializer(){
        super(SecurityConfig.class);
    }

}

Controller.java

@Controller
public class HomeController {

   @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(Model model) {
        return "login";
    }

    @RequestMapping("/success")
    public String loginPageRedirect(HttpServletRequest httpServletRequest){

        if(httpServletRequest.isUserInRole("ROLE_ADMIN")) {
            return "index1";
        } else if(httpServletRequest.isUserInRole("ROLE_USER")) {
            return "index2";
        } else {
            return "index3";
        }
    }
}

当我成功登录后,我的用户被重定向到错误的页面。我的用户具有角色 ROLE_USER 但方法 loginPageRedirect 将他重定向到页面 index3,而它应该是 index2。我想我的用户角色无法识别。我怎样才能做到这一点?我应该向 loginPageRedirect 添加一些东西作为参数,以便它识别角色吗?

【问题讨论】:

  • 请您检查一下:在/success控制器方法中添加参数Authentication authentication,spring mvc会自动向其中注入身份验证。并且在这个方法里面打印所有的`authentication.getAuthorities()`,看看角色是否正确
  • @sunkuet02 是的,它像这样显示角色 [ROLE_USER]。但我仍然有错误的重定向。
  • 尝试在loginPageRedirect方法的最开始添加httpServletRequest.authenticate(response);。用这个控制器添加一个额外的参数HttpServletResponse response
  • @sunkuet02 你是什么意思?你能写代码让我更清楚吗?
  • loginPageRedirect方法的最开始添加httpServletRequest.authenticate(response);。用这个控制器添加一个额外的参数HttpServletResponse response

标签: spring spring-security


【解决方案1】:

我找到了适合我的解决方案。

我像这样编辑了我的 loginPageRedirect 方法:

@RequestMapping("/success")
    public void loginPageRedirect(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException, ServletException {

        String role =  authResult.getAuthorities().toString();

        if(role.contains("ROLE_ADMIN")){
         response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/index1"));                            
         }
         else if(role.contains("ROLE_USER")) {
             response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/index2"));
         }
    }

希望它能帮助遇到同样问题的人:)

【讨论】:

  • +1 看起来不错。 Authentication 毕竟帮助它。然后您可以查看我对您的 privious 问题的最后回答
  • @sunkuet02 谢谢你的朋友! :) 我还发布了有关该问题的完整答案,请看一下。
猜你喜欢
  • 2016-09-03
  • 2021-08-03
  • 2022-08-24
  • 2023-03-22
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 2013-04-26
  • 2011-07-21
相关资源
最近更新 更多