55import com .codingapi .springboot .security .exception .TokenExpiredException ;
66import com .codingapi .springboot .security .jwt .Jwt ;
77import com .codingapi .springboot .security .jwt .Token ;
8+ import jakarta .servlet .FilterChain ;
9+ import jakarta .servlet .ServletException ;
10+ import jakarta .servlet .http .HttpServletRequest ;
11+ import jakarta .servlet .http .HttpServletResponse ;
812import lombok .extern .slf4j .Slf4j ;
913import org .apache .commons .io .IOUtils ;
14+ import org .springframework .security .authentication .AuthenticationDetailsSource ;
1015import org .springframework .security .authentication .AuthenticationManager ;
16+ import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1117import org .springframework .security .core .context .SecurityContextHolder ;
18+ import org .springframework .security .web .authentication .www .BasicAuthenticationConverter ;
1219import org .springframework .security .web .authentication .www .BasicAuthenticationFilter ;
20+ import org .springframework .util .Assert ;
1321import org .springframework .util .StringUtils ;
1422
15- import jakarta .servlet .FilterChain ;
16- import jakarta .servlet .ServletException ;
17- import jakarta .servlet .http .HttpServletRequest ;
18- import jakarta .servlet .http .HttpServletResponse ;
1923import java .io .IOException ;
24+ import java .nio .charset .Charset ;
2025import java .nio .charset .StandardCharsets ;
2126
2227@ Slf4j
@@ -26,15 +31,33 @@ public class MyAuthenticationFilter extends BasicAuthenticationFilter {
2631
2732 private final Jwt jwt ;
2833
34+ private final BasicAuthenticationConverter authenticationConverter = new BasicAuthenticationConverter ();
35+
2936 public MyAuthenticationFilter (AuthenticationManager authenticationManager , Jwt jwt ) {
3037 super (authenticationManager );
3138 this .jwt = jwt ;
3239 }
3340
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+
3450 @ Override
3551 protected void doFilterInternal (HttpServletRequest request , HttpServletResponse response , FilterChain chain ) throws IOException , ServletException {
3652 log .debug ("token authentication ~" );
3753
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 ;
59+ }
60+
3861 String sign = request .getHeader (TOKEN_KEY );
3962 if (!StringUtils .hasLength (sign )) {
4063 writeResponse (response , Response .buildFailure ("token.error" , "token must not null." ));
0 commit comments