How do I remove the ROLE_ prefix from Spring Security with JavaConfig?

后端 未结 7 1078
灰色年华
灰色年华 2020-12-01 16:22

I\'m trying to remove the \"ROLE_\" prefix in Spring Security. The first thing I tried was:

http.servletApi().rolePrefix(\"\");

That didn\'

相关标签:
7条回答
  • 2020-12-01 17:08

    The following configuration works for me.

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.expressionHandler(new DefaultWebSecurityExpressionHandler() {
            @Override
            protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, FilterInvocation fi) {
                WebSecurityExpressionRoot root = (WebSecurityExpressionRoot) super.createSecurityExpressionRoot(authentication, fi);
                root.setDefaultRolePrefix(""); //remove the prefix ROLE_
                return root;
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题