程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Spring - 5 getAuthorities() still uses "ROLE_" prefix

发布于2022-09-30 20:07     阅读(628)     评论(0)     点赞(0)     收藏(3)


Dear all I am using spring 5 and like to have the ROLE_ prefix removed. Thereforre I used "grantedAuthorityDefaults" and set the role prefix to "". Unfortunatley when I call later on SecurityContextHolder.getContext().getAuthentication().getAuthorities() on a page without login (Public acces) than I still get values with "ROLE_" prefix "ROLE_ANONYMOUS" and my mapping logic in the JSON Rest controller Advice fails.

Any hits at which other places I ned to declare the ROLE prefix = "" ?

@Configuration
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true)
public class WebSecurityJwtConfiguration extends WebSecurityConfigurerAdapter {
    ...
    @Bean
    public GrantedAuthorityDefaults grantedAuthorityDefaults() {
        return new GrantedAuthorityDefaults("");
    }
    ...
}
@RestControllerAdvice
public class CoreSecurityJsonViewControllerAdviceimplements ResponseBodyAdvice<Object> {

    protected void beforeBodyWriteInternal(MappingJacksonValue mappingJacksonValue, MediaType mediaType, MethodParameter methodParameter, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        if (SecurityContextHolder.getContext().getAuthentication() != null && SecurityContextHolder.getContext().getAuthentication().getAuthorities() != null) {

           // HERE I still get values with "ROLE_" prefix

            Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
            Class prioritizedJsonView = this.getJsonViews(authorities);
            if (prioritizedJsonView != null) {
                mappingJacksonValue.setSerializationView(prioritizedJsonView);
            }
        }
    }

    protected Class getJsonViews(Collection<? extends GrantedAuthority> authorities) {
        Optional var10000 = authorities.stream().map(GrantedAuthority::getAuthority).map(Role::valueOf).max(Comparator.comparing(Enum::ordinal));
        Map var10001 = View.MAPPING;
        var10001.getClass();
        return (Class)var10000.map(var10001::get).orElse((Object)null);
    }

    @Override
    protected Class getJsonViews(Collection<? extends GrantedAuthority> authorities) {
        return authorities.stream()
                .map(GrantedAuthority::getAuthority)
                .map(Role::valueOf)
                .max(Comparator.comparing(Role::ordinal))
                .map(View.MAPPING::get)
                .orElse(null);
    }
}

解决方案


Yes, that is because the AnonymousAuthenticationFilter Spring Security uses has the "ROLE_ANONYMOUS" hardcoded inside.

You can change that behavior with this override in your WebSecurityConfigurerAdapter

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.anonymous().authorities("ANONYMOUS"); // or your custom role
}


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/526539/65afef9417b3c851f3ea/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)