View Javadoc
1   package cn.home1.oss.lib.common;
2   
3   import com.fasterxml.jackson.core.JsonGenerator;
4   import com.fasterxml.jackson.core.JsonParser;
5   import com.fasterxml.jackson.core.JsonToken;
6   import com.fasterxml.jackson.databind.DeserializationContext;
7   import com.fasterxml.jackson.databind.JsonDeserializer;
8   import com.fasterxml.jackson.databind.JsonSerializer;
9   import com.fasterxml.jackson.databind.ObjectMapper;
10  import com.fasterxml.jackson.databind.SerializerProvider;
11  import com.fasterxml.jackson.databind.module.SimpleModule;
12  
13  import lombok.extern.slf4j.Slf4j;
14  
15  import org.springframework.core.env.PropertyResolver;
16  
17  import java.io.IOException;
18  
19  @Slf4j
20  public class SpringSecurityJackson2Module extends SimpleModule {
21  
22    private static final long serialVersionUID = 1L;
23  
24    public SpringSecurityJackson2Module() {
25      super();
26      try {
27        // TODO remove all SimpleGrantedAuthority.
28        Class.forName("org.springframework.security.core.GrantedAuthority");
29        addSerializer(org.springframework.security.core.GrantedAuthority.class, new GrantedAuthoritySerializer());
30        addDeserializer(org.springframework.security.core.GrantedAuthority.class, new GrantedAuthorityDeserializer());
31        addSerializer(org.springframework.security.core.authority.SimpleGrantedAuthority.class, //
32          new GrantedAuthoritySerializer());
33        addDeserializer(org.springframework.security.core.authority.SimpleGrantedAuthority.class, //
34          new GrantedAuthorityDeserializer());
35      } catch (final ClassNotFoundException ignored) {
36        log.debug("class not found, skip SpringSecurityJackson2Module", ignored);
37      }
38    }
39  
40    /**
41     * jackson2 module for spring security (
42     * {@link org.springframework.security.core.GrantedAuthority},
43     * {@link org.springframework.security.core.authority.SimpleGrantedAuthority}
44     * ).
45     *
46     * @author zhanghaolun
47     */
48    public enum Jackson2SpringSecurityConfigurator implements Jackson2Configurator<Jackson2Configurator.BuildinJackson2Configurators> {
49      JACKSON2_SPRINGSECURITY_CONFIGURATOR {
50        @Override
51        public <M extends ObjectMapper> M config(final PropertyResolver propertyResolver, final M mapper) {
52          mapper.registerModule(new SpringSecurityJackson2Module());
53          return mapper;
54        }
55      };
56  
57      @Override
58      public abstract <M extends ObjectMapper> M config(PropertyResolver propertyResolver, M mapper);
59    }
60  
61    public static class GrantedAuthoritySerializer //
62      extends JsonSerializer<org.springframework.security.core.GrantedAuthority> {
63  
64      @Override
65      public void serialize( //
66        final org.springframework.security.core.GrantedAuthority value, //
67        final JsonGenerator jsonGenerator, //
68        final SerializerProvider provider //
69      ) throws IOException {
70        jsonGenerator.writeString(value.toString());
71      }
72    }
73  
74    public static class GrantedAuthorityDeserializer //
75      extends JsonDeserializer<org.springframework.security.core.authority.SimpleGrantedAuthority> {
76  
77      @Override
78      public org.springframework.security.core.authority.SimpleGrantedAuthority deserialize( //
79        final JsonParser jsonParser, //
80        final DeserializationContext deserializationContext //
81      ) throws IOException {
82        final org.springframework.security.core.authority.SimpleGrantedAuthority grantedAuthority;
83        final JsonToken currentToken = jsonParser.getCurrentToken();
84  
85        if (JsonToken.VALUE_STRING.equals(currentToken)) {
86          try {
87            final String grantedAuthorityString = jsonParser.getText().trim();
88            grantedAuthority = new org.springframework.security.core.authority.SimpleGrantedAuthority( //
89              grantedAuthorityString);
90            return grantedAuthority;
91          } catch (final Exception translated) {
92            throw new Jackson2Utils.RuntimeJsonProcessingException("couldn't parse GrantedAuthority", translated);
93          }
94        } else {
95          throw new IllegalStateException();
96        }
97      }
98    }
99  }