Spring Security定义自定义匿名主体

时间:2015-08-29 11:02:05

标签: authentication spring-security

如何为每个匿名制作自定义唯一匿名主体而不是默认anonymousUser

例如,请增加anonymousUser1anonymousUser2

我想我必须覆盖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;
    }
}

0 个答案:

没有答案