1818import lombok .RequiredArgsConstructor ;
1919import me .zhengjie .annotation .AnonymousAccess ;
2020import me .zhengjie .modules .security .security .*;
21+ import me .zhengjie .utils .enums .RequestMethodEnum ;
2122import org .springframework .context .ApplicationContext ;
2223import org .springframework .context .annotation .Bean ;
2324import org .springframework .context .annotation .Configuration ;
3132import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
3233import org .springframework .security .crypto .password .PasswordEncoder ;
3334import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
35+ import org .springframework .web .bind .annotation .RequestMethod ;
3436import org .springframework .web .filter .CorsFilter ;
3537import org .springframework .web .method .HandlerMethod ;
3638import org .springframework .web .servlet .mvc .method .RequestMappingInfo ;
3739import org .springframework .web .servlet .mvc .method .annotation .RequestMappingHandlerMapping ;
38- import java .util .HashSet ;
39- import java .util .Map ;
40- import java .util .Set ;
40+ import java .util .*;
4141
4242/**
4343 * @author Zheng Jie
@@ -70,14 +70,8 @@ public PasswordEncoder passwordEncoder() {
7070 protected void configure (HttpSecurity httpSecurity ) throws Exception {
7171 // 搜寻匿名标记 url: @AnonymousAccess
7272 Map <RequestMappingInfo , HandlerMethod > handlerMethodMap = applicationContext .getBean (RequestMappingHandlerMapping .class ).getHandlerMethods ();
73- Set <String > anonymousUrls = new HashSet <>();
74- for (Map .Entry <RequestMappingInfo , HandlerMethod > infoEntry : handlerMethodMap .entrySet ()) {
75- HandlerMethod handlerMethod = infoEntry .getValue ();
76- AnonymousAccess anonymousAccess = handlerMethod .getMethodAnnotation (AnonymousAccess .class );
77- if (null != anonymousAccess ) {
78- anonymousUrls .addAll (infoEntry .getKey ().getPatternsCondition ().getPatterns ());
79- }
80- }
73+ // 获取匿名标记
74+ Map <String , Set <String >> anonymousUrls = getAnonymousUrl (handlerMethodMap );
8175 httpSecurity
8276 // 禁用 CSRF
8377 .csrf ().disable ()
@@ -86,18 +80,15 @@ protected void configure(HttpSecurity httpSecurity) throws Exception {
8680 .exceptionHandling ()
8781 .authenticationEntryPoint (authenticationErrorHandler )
8882 .accessDeniedHandler (jwtAccessDeniedHandler )
89-
9083 // 防止iframe 造成跨域
9184 .and ()
9285 .headers ()
9386 .frameOptions ()
9487 .disable ()
95-
9688 // 不创建会话
9789 .and ()
9890 .sessionManagement ()
9991 .sessionCreationPolicy (SessionCreationPolicy .STATELESS )
100-
10192 .and ()
10293 .authorizeRequests ()
10394 // 静态资源等等
@@ -121,13 +112,69 @@ protected void configure(HttpSecurity httpSecurity) throws Exception {
121112 .antMatchers ("/druid/**" ).permitAll ()
122113 // 放行OPTIONS请求
123114 .antMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
124- // 自定义匿名访问所有url放行 : 允许匿名和带权限以及登录用户访问
125- .antMatchers (anonymousUrls .toArray (new String [0 ])).permitAll ()
115+ // 自定义匿名访问所有url放行:允许匿名和带Token访问,细腻化到每个 Request 类型
116+ // GET
117+ .antMatchers (HttpMethod .GET , anonymousUrls .get (RequestMethodEnum .GET .getType ()).toArray (new String [0 ])).permitAll ()
118+ // POST
119+ .antMatchers (HttpMethod .POST , anonymousUrls .get (RequestMethodEnum .POST .getType ()).toArray (new String [0 ])).permitAll ()
120+ // PUT
121+ .antMatchers (HttpMethod .PUT , anonymousUrls .get (RequestMethodEnum .PUT .getType ()).toArray (new String [0 ])).permitAll ()
122+ // PATCH
123+ .antMatchers (HttpMethod .PATCH , anonymousUrls .get (RequestMethodEnum .PATCH .getType ()).toArray (new String [0 ])).permitAll ()
124+ // DELETE
125+ .antMatchers (HttpMethod .DELETE , anonymousUrls .get (RequestMethodEnum .DELETE .getType ()).toArray (new String [0 ])).permitAll ()
126+ // 所有类型的接口都放行
127+ .antMatchers (anonymousUrls .get (RequestMethodEnum .ALL .getType ()).toArray (new String [0 ])).permitAll ()
126128 // 所有请求都需要认证
127129 .anyRequest ().authenticated ()
128130 .and ().apply (securityConfigurerAdapter ());
129131 }
130132
133+ private Map <String , Set <String >> getAnonymousUrl (Map <RequestMappingInfo , HandlerMethod > handlerMethodMap ) {
134+ Map <String , Set <String >> anonymousUrls = new HashMap <>();
135+ Set <String > get = new HashSet <>();
136+ Set <String > post = new HashSet <>();
137+ Set <String > put = new HashSet <>();
138+ Set <String > patch = new HashSet <>();
139+ Set <String > delete = new HashSet <>();
140+ Set <String > all = new HashSet <>();
141+ for (Map .Entry <RequestMappingInfo , HandlerMethod > infoEntry : handlerMethodMap .entrySet ()) {
142+ HandlerMethod handlerMethod = infoEntry .getValue ();
143+ AnonymousAccess anonymousAccess = handlerMethod .getMethodAnnotation (AnonymousAccess .class );
144+ if (null != anonymousAccess ) {
145+ List <RequestMethod > requestMethods = new ArrayList <>(infoEntry .getKey ().getMethodsCondition ().getMethods ());
146+ RequestMethodEnum request = RequestMethodEnum .find (requestMethods .size () == 0 ? RequestMethodEnum .ALL .getType () : requestMethods .get (0 ).name ());
147+ switch (Objects .requireNonNull (request )){
148+ case GET :
149+ get .addAll (infoEntry .getKey ().getPatternsCondition ().getPatterns ());
150+ break ;
151+ case POST :
152+ post .addAll (infoEntry .getKey ().getPatternsCondition ().getPatterns ());
153+ break ;
154+ case PUT :
155+ put .addAll (infoEntry .getKey ().getPatternsCondition ().getPatterns ());
156+ break ;
157+ case PATCH :
158+ patch .addAll (infoEntry .getKey ().getPatternsCondition ().getPatterns ());
159+ break ;
160+ case DELETE :
161+ delete .addAll (infoEntry .getKey ().getPatternsCondition ().getPatterns ());
162+ break ;
163+ default :
164+ all .addAll (infoEntry .getKey ().getPatternsCondition ().getPatterns ());
165+ break ;
166+ }
167+ }
168+ }
169+ anonymousUrls .put (RequestMethodEnum .GET .getType (), get );
170+ anonymousUrls .put (RequestMethodEnum .POST .getType (), post );
171+ anonymousUrls .put (RequestMethodEnum .PUT .getType (), put );
172+ anonymousUrls .put (RequestMethodEnum .PATCH .getType (), patch );
173+ anonymousUrls .put (RequestMethodEnum .DELETE .getType (), delete );
174+ anonymousUrls .put (RequestMethodEnum .ALL .getType (), all );
175+ return anonymousUrls ;
176+ }
177+
131178 private TokenConfigurer securityConfigurerAdapter () {
132179 return new TokenConfigurer (tokenProvider );
133180 }
0 commit comments