[英]how to get current logged in role by alert box in jsp and spring security
[英]How to get the role that a user logged in with in my controller using 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> </td></tr>
<tr><td colspan=2></td></tr>
</table>
</form>
控制器代碼:
@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
}
}
在這里,我獲取了哪個用戶已登錄,並且我正在檢索他們的角色,如我的 spring-security.xml 文件中所定義的 - 例如 [role1 , role2]。 這不會檢索用戶登錄時使用的角色。 通過用戶登錄的用戶名,然后我從數據庫中獲取角色並重定向到相應的頁面。 如果用戶只有一個角色,它工作正常,但如果他們有多個角色,那么我不清楚我將如何重定向他們。 所以,我認為一旦登錄用戶可以選擇角色,並在此基礎上我將重定向。
Spring-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>
每個角色都有不同的視圖頁面,所以如果我使用 user1 登錄並從下拉列表中選擇 role2 我將如何在我的控制器中獲得這個 role2,以便我可以將 user1 重定向到相應的 jsp 頁面?
如果有更好的方法來實現這一點,請告訴我。
將Authentication
注入您的 Controller 而不是Principal
。
@RequestMapping(value="/home", method = RequestMethod.GET)
public ModelAndView printWelcome(ModelMap model, Authentication authentication) {
}
authentication.getAuthorities();
現在將返回您的角色。
這是我在控制器中的解決方案。 我的項目默認有兩個頁面。 一個頁面是受歡迎的,其他頁面默認是一個儀表板,只有當用戶包含角色 ROLE DASHBOARD 時才能訪問。
@RequestMapping(value="/redirectToPageDefault",method=RequestMethod.GET)
public ModelAndView redirectToPageDefault(SecurityContextHolder auth){
Collection<?extends GrantedAuthority> granted = auth.getContext().getAuthentication().getAuthorities();
String role;
//set page default to rules common
ModelAndView mav = new ModelAndView("empty");
for(int i=0;i<granted.size();i++){
role = granted.toArray()[i] + "";
logger.info("role verified" + i + " is -> " + role);
//verify if user contain role to view dashboard page default
if(role.equals("ROLE_DASHBOARD")){
logger.warn("IDENTIFIED: ROLE_DASHBOARD = " + role );
mav.setViewName("dasboard");
}
}
return mav;
}
嘗試以下 2 項之一:
1. 將 java.security.Principal 傳遞給您的任何 Controller 方法。
2. 通過 SecurityContextHolder.getContext().getAuthentication() 獲取登錄用戶
您可以通過此方法檢查角色
private boolean hasRole(String role) {
Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>)
SecurityContextHolder.getContext().getAuthentication().getAuthorities();
boolean hasRole = false;
for (GrantedAuthority authority : authorities) {
hasRole = authority.getAuthority().equals(role);
if (hasRole) {
break;
}
}
return hasRole;
}
您也可以看到這篇文章: 如何使用 Spring Security 訪問角色和用戶詳細信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.