나는 REST 서비스 및 스프링 보안과 함께 작동하는 애플리케이션을 가지고있다. 기본 인증이 있고 하드 및 소프트 로그인이 필요합니다.스프링 보안 : 인증 된 사용자에게 특별한 역할을 추가하는 방법
시나리오 : 사용자가 로그인 할 때 ROLE_SOFT 이 할당되고 ROLE_SOFT가 필요한 URL에 액세스 할 수 있지만 ROLE_HARD가 필요한 URL에 액세스하려는 경우에는 일부 코드를 지정된 웹 서비스.
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();
}
}
하지만 내 세션이 중단 인증 변경 한 후 .. 어쩌면 몇 가지 :
그래서 난 후이 Acegi Security: How do i add another GrantedAuthority to Authentication to anonymous user
내가 내를 생성, 읽기 하나 더 나은 솔루션을 알고 ...