如何为每个匿名制作自定义唯一匿名主体而不是默认anonymousUser
?
例如,请增加anonymousUser1
,anonymousUser2
。
我想我必须覆盖AnonymousAuthenticationFilter
。
这是正确的方法吗?
@Component
public class AnonymousAuthentication extends AnonymousAuthenticationFilter {
private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new WebAuthenticationDetailsSource();
private static int anonymSuffix = 0;
public AnonymousAuthentication() {
super("key");
}
@Override
protected Authentication createAuthentication(HttpServletRequest request) {
anonymSuffix++;
AnonymousAuthenticationToken auth = new AnonymousAuthenticationToken("",
"anonym" + anonymSuffix, AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
auth.setDetails(authenticationDetailsSource.buildDetails(request));
return auth;
}
}