55import com .codingapi .springboot .security .exception .TokenExpiredException ;
66import com .codingapi .springboot .security .jwt .Jwt ;
77import com .codingapi .springboot .security .jwt .Token ;
8+ import com .codingapi .springboot .security .properties .SecurityJwtProperties ;
89import jakarta .servlet .FilterChain ;
910import jakarta .servlet .ServletException ;
1011import jakarta .servlet .http .HttpServletRequest ;
1112import jakarta .servlet .http .HttpServletResponse ;
1213import lombok .extern .slf4j .Slf4j ;
1314import org .apache .commons .io .IOUtils ;
14- import org .springframework .security .authentication .AuthenticationDetailsSource ;
1515import org .springframework .security .authentication .AuthenticationManager ;
16- import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1716import org .springframework .security .core .context .SecurityContextHolder ;
18- import org .springframework .security .web .authentication .www .BasicAuthenticationConverter ;
1917import org .springframework .security .web .authentication .www .BasicAuthenticationFilter ;
20- import org .springframework .util .Assert ;
18+ import org .springframework .util .AntPathMatcher ;
2119import org .springframework .util .StringUtils ;
20+ import org .springframework .web .filter .OncePerRequestFilter ;
2221
2322import java .io .IOException ;
24- import java .nio .charset .Charset ;
2523import java .nio .charset .StandardCharsets ;
2624
2725@ Slf4j
@@ -31,54 +29,44 @@ public class MyAuthenticationFilter extends BasicAuthenticationFilter {
3129
3230 private final Jwt jwt ;
3331
34- private final BasicAuthenticationConverter authenticationConverter = new BasicAuthenticationConverter ();
32+ private final SecurityJwtProperties securityJwtProperties ;
33+ private final AntPathMatcher antPathMatcher = new AntPathMatcher ();
3534
36- public MyAuthenticationFilter (AuthenticationManager authenticationManager , Jwt jwt ) {
37- super (authenticationManager );
35+ public MyAuthenticationFilter (AuthenticationManager manager , SecurityJwtProperties securityJwtProperties , Jwt jwt ) {
36+ super (manager );
3837 this .jwt = jwt ;
38+ this .securityJwtProperties = securityJwtProperties ;
3939 }
4040
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- }
4941
5042 @ Override
5143 protected void doFilterInternal (HttpServletRequest request , HttpServletResponse response , FilterChain chain ) throws IOException , ServletException {
5244 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+ }
5969 }
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-
8270 chain .doFilter (request , response );
8371
8472 }
@@ -87,4 +75,6 @@ private void writeResponse(HttpServletResponse servletResponse, Response returnR
8775 String content = JSONObject .toJSONString (returnResponse );
8876 IOUtils .write (content , servletResponse .getOutputStream (), StandardCharsets .UTF_8 );
8977 }
78+
79+
9080}
0 commit comments