如何使用Spring Security获取用户在我的控制器中登录的角色

use*_*r07 4 java authentication spring spring-mvc spring-security

我正在尝试实现Spring安全功能以进行授权和身份验证.我正面临一些有关用户角色的问题.我希望在我的控制器中检查用户登录的角色,以便根据角色将用户重定向到相应的页面(因为用户可以有多个角色).但是,我没有在我的Java控制器中获得登录角色.

登录jsp页面

   <form action="<c:url value='j_spring_security_check'/>"
                method="POST" name='loginForm'>
 <table class="center">
 <tr><td>User Name:  </td><td><input type="text" name="j_username"  id="userName" ></td></tr>
 <tr><td>Password:  </td><td><input type="password" name="j_password" id="password"></td></tr> 
 <tr><td>Role: </td><td>
 <select id="role" name="j_role">  
 <option style="font-weight: bold;">Select Roll </option>
 <option id="role1" value="role1">Role 1</option>
 <option id="role2" value="role2">Role 2 </option>
 <option id="role3" value="role3">Role 3 </option>
 </select></td></tr>

 <tr><td><input type="submit" value="Login" style="height: 25px; width: 100px"></td><td><input type="reset" value="Reset" style="height: 25px; width: 100px"></td></tr>
 <tr><td colspan=2>&nbsp;</td></tr>

  <tr><td colspan=2></td></tr>
 </table>
 </form>
Run Code Online (Sandbox Code Playgroud)

控制器代码:

@RequestMapping(value="/home", method = RequestMethod.GET)
 public ModelAndView printWelcome(ModelMap model, Principal principal ) throws SQLException {

     ModelAndView mav = new ModelAndView(); 
try{
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
     String n= auth.getName();
    String r= auth.getAuthorities().toString();

     System.out.println("the value of username is "+n);
    System.out.println("the value of role is  "+r);

     returning result
  } 
}
Run Code Online (Sandbox Code Playgroud)

在这里,我得到了哪个用户已登录,并且我正在检索他们的角色,如我的spring-security.xml文件中所定义的那样 - 例如[role1,role2].这不会检索用户登录的角色.通过用户使用我登录的用户名,然后从数据库中获取角色并重定向到相应的页面.如果用户只有一个角色,它可以正常工作,但如果他们有多个角色,那么我不清楚我将如何重定向它们.所以,我认为一旦登录用户就可以选择角色,在此基础上我将重定向.

弹簧security.xml文件

   <http auto-config="true"  use-expressions="true">
 <intercept-url pattern="/home" access="hasAnyRole('role1','role2','role3')"/>

<form-login login-page="/login" default-target-url="/home" authentication-failure-url="/loginError" />


</http>
<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <user-service>

            <user name="user1" password="123" authorities="role1,role2" />   
      <user name="user2" password="123" authorities="role2,role3" />   
            <user name="stephen" password="123" authorities="role1" />
            <user name="robert" password="123" authorities="role3" />
        </user-service>
    </authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)

每个角色都有不同的视图页面,所以如果我使用user1登录并从下拉列表中选择role2,我将如何在我的控制器中获取此角色2,以便我可以将user1重定向到相应的jsp页面?

如果有更好的方法来实现这一点,请告诉我.

Dav*_*der 8

注入一个Authentication控制器,而不是Principal.

@RequestMapping(value="/home", method = RequestMethod.GET)
public ModelAndView printWelcome(ModelMap model, Authentication authentication) {
}
Run Code Online (Sandbox Code Playgroud)

authentication.getAuthorities(); 现在将返回您的角色.


小智 2

尝试以下 2 种方法之一:
1. 将 java.security.Principal 传递给您的任意一个 Controller 方法。
2.通过SecurityContextHolder.getContext().getAuthentication()获取登录用户