5
5
import com .codingapi .springboot .security .exception .TokenExpiredException ;
6
6
import com .codingapi .springboot .security .jwt .Jwt ;
7
7
import com .codingapi .springboot .security .jwt .Token ;
8
+ import com .codingapi .springboot .security .properties .SecurityJwtProperties ;
8
9
import jakarta .servlet .FilterChain ;
9
10
import jakarta .servlet .ServletException ;
10
11
import jakarta .servlet .http .HttpServletRequest ;
11
12
import jakarta .servlet .http .HttpServletResponse ;
12
13
import lombok .extern .slf4j .Slf4j ;
13
14
import org .apache .commons .io .IOUtils ;
14
- import org .springframework .security .authentication .AuthenticationDetailsSource ;
15
15
import org .springframework .security .authentication .AuthenticationManager ;
16
- import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
17
16
import org .springframework .security .core .context .SecurityContextHolder ;
18
- import org .springframework .security .web .authentication .www .BasicAuthenticationConverter ;
19
17
import org .springframework .security .web .authentication .www .BasicAuthenticationFilter ;
20
- import org .springframework .util .Assert ;
18
+ import org .springframework .util .AntPathMatcher ;
21
19
import org .springframework .util .StringUtils ;
20
+ import org .springframework .web .filter .OncePerRequestFilter ;
22
21
23
22
import java .io .IOException ;
24
- import java .nio .charset .Charset ;
25
23
import java .nio .charset .StandardCharsets ;
26
24
27
25
@ Slf4j
@@ -31,54 +29,44 @@ public class MyAuthenticationFilter extends BasicAuthenticationFilter {
31
29
32
30
private final Jwt jwt ;
33
31
34
- private final BasicAuthenticationConverter authenticationConverter = new BasicAuthenticationConverter ();
32
+ private final SecurityJwtProperties securityJwtProperties ;
33
+ private final AntPathMatcher antPathMatcher = new AntPathMatcher ();
35
34
36
- public MyAuthenticationFilter (AuthenticationManager authenticationManager , Jwt jwt ) {
37
- super (authenticationManager );
35
+ public MyAuthenticationFilter (AuthenticationManager manager , SecurityJwtProperties securityJwtProperties , Jwt jwt ) {
36
+ super (manager );
38
37
this .jwt = jwt ;
38
+ this .securityJwtProperties = securityJwtProperties ;
39
39
}
40
40
41
- public void setAuthenticationDetailsSource (AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource ) {
42
- this .authenticationConverter .setAuthenticationDetailsSource (authenticationDetailsSource );
43
- }
44
-
45
- public void setCredentialsCharset (String credentialsCharset ) {
46
- Assert .hasText (credentialsCharset , "credentialsCharset cannot be null or empty" );
47
- this .authenticationConverter .setCredentialsCharset (Charset .forName (credentialsCharset ));
48
- }
49
41
50
42
@ Override
51
43
protected void doFilterInternal (HttpServletRequest request , HttpServletResponse response , FilterChain chain ) throws IOException , ServletException {
52
44
log .debug ("token authentication ~" );
53
-
54
- UsernamePasswordAuthenticationToken authRequest = authenticationConverter .convert (request );
55
- if (authRequest == null ) {
56
- this .logger .trace ("Did not process authentication request since failed to find username and password in Basic Authorization header" );
57
- chain .doFilter (request , response );
58
- return ;
45
+ for (String antUrl : securityJwtProperties .getAuthenticatedUrls ()) {
46
+ if (antPathMatcher .match (antUrl ,request .getRequestURI ())) {
47
+
48
+ String sign = request .getHeader (TOKEN_KEY );
49
+ if (!StringUtils .hasLength (sign )) {
50
+ writeResponse (response , Response .buildFailure ("token.error" , "token must not null." ));
51
+ return ;
52
+ }
53
+
54
+ Token token = jwt .parser (sign );
55
+ if (token .canRestToken ()) {
56
+ Token newSign = jwt .create (token .getUsername (), token .decodeIv (), token .getAuthorities (), token .getExtra ());
57
+ log .info ("reset token " );
58
+ response .setHeader (TOKEN_KEY , newSign .getToken ());
59
+ }
60
+ try {
61
+ token .verify ();
62
+ } catch (TokenExpiredException e ) {
63
+ writeResponse (response , Response .buildFailure ("token.expire" , "token expire." ));
64
+ return ;
65
+ }
66
+
67
+ SecurityContextHolder .getContext ().setAuthentication (token .getAuthenticationToken ());
68
+ }
59
69
}
60
-
61
- String sign = request .getHeader (TOKEN_KEY );
62
- if (!StringUtils .hasLength (sign )) {
63
- writeResponse (response , Response .buildFailure ("token.error" , "token must not null." ));
64
- return ;
65
- }
66
-
67
- Token token = jwt .parser (sign );
68
- if (token .canRestToken ()) {
69
- Token newSign = jwt .create (token .getUsername (), token .decodeIv (), token .getAuthorities (),token .getExtra ());
70
- log .info ("reset token " );
71
- response .setHeader (TOKEN_KEY , newSign .getToken ());
72
- }
73
- try {
74
- token .verify ();
75
- } catch (TokenExpiredException e ) {
76
- writeResponse (response , Response .buildFailure ("token.expire" , "token expire." ));
77
- return ;
78
- }
79
-
80
- SecurityContextHolder .getContext ().setAuthentication (token .getAuthenticationToken ());
81
-
82
70
chain .doFilter (request , response );
83
71
84
72
}
@@ -87,4 +75,6 @@ private void writeResponse(HttpServletResponse servletResponse, Response returnR
87
75
String content = JSONObject .toJSONString (returnResponse );
88
76
IOUtils .write (content , servletResponse .getOutputStream (), StandardCharsets .UTF_8 );
89
77
}
78
+
79
+
90
80
}
0 commit comments