![](/img/trans.png)
[英]Spring security web check if currently authenticated user has role
[英]Spring Security: How To add extra role to authenticated user
我有一個與REST服務和spring security一起使用的應用程序。 我具有基本身份驗證,並且需要進行硬登錄和軟登錄。
方案是:當用戶登錄時,他被分配了ROLE_SOFT並可以訪問需要ROLE_SOFT的URL,但是如果他想訪問需要ROLE_HARD的URL,則必須向指定的Web服務發送一些代碼或某些內容。
因此,我閱讀了此Acegi Security:如何為身份驗證向匿名用戶添加另一個GrantedAuthority
之后,我創建我的:
public class AuthenticationWrapper implements Authentication
{
private Authentication original;
public AuthenticationWrapper(Authentication original)
{
this.original = original;
}
public String getName() { return original.getName(); }
public Object getCredentials() { return original.getCredentials(); }
public Object getDetails() { return original.getDetails(); }
public Object getPrincipal() { return original.getPrincipal(); }
public boolean isAuthenticated() { return original.isAuthenticated(); }
public void setAuthenticated( boolean isAuthenticated ) throws IllegalArgumentException
{
original.setAuthenticated( isAuthenticated );
}
public Collection<? extends GrantedAuthority> getAuthorities() {
System.out.println("EXISTING ROLES:");
System.out.println("Size=:"+original.getAuthorities().size());
for (GrantedAuthority iterable : original.getAuthorities()) {
System.out.println(iterable.getAuthority());
}
GrantedAuthority newrole = new SimpleGrantedAuthority("ROLE_HARD");
System.out.println("ADD new ROLE:"+newrole.getAuthority());
Collection<? extends GrantedAuthority> originalRoles = original.getAuthorities();
ArrayList<GrantedAuthority> temp = new ArrayList<GrantedAuthority>(originalRoles.size()+1);
temp.addAll(originalRoles);
temp.add(newrole);
System.out.println("RETURN NEW LIST SIZE"+temp.size());
for (GrantedAuthority grantedAuthority : temp) {
System.out.println("NEW ROLES:"+grantedAuthority.getAuthority());
}
return Collections.unmodifiableList(temp);
}
和控制器
@Controller
@RequestMapping("/login")
public class LoginControllerImpl implements LoginController {
LoginService loginService;
@RequestMapping(method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public User getUserSettings(){
loginService=new LoginServiceImpl();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
AuthenticationWrapper wrapper = new AuthenticationWrapper(auth);
SecurityContextHolder.getContext().setAuthentication( wrapper );
return loginService.getUser();
}
}
但是在更改身份驗證后,我的會話中斷了。.也許有人知道更好的解決方案...
只是一個主意。如果用戶第一次使用登錄表單登錄並需要訪問資源向導,則需要其他權限,那么為什么不第二次將用戶重定向回登錄頁面呢?
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/resources/**" access="denyAll"/>
<intercept-url pattern="/login.do" access="permitAll"/>
<intercept-url pattern="/role_soft_url_domain/* " access="hasRole('ROLE_SOFT') and fullyAuthenticated"/>
<intercept-url pattern="/role_hard_url_domain/*" access="hasRole('ROLE_HARD') and fullyAuthenticated"/>
<intercept-url pattern="/*" access="hasRole('ROLE_SOFT')"/>
<form-login login-page="/login.do" />
<logout invalidate-session="true"
logout-success-url="/"
logout-url="/j_spring_security_logout"/>
</http>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.