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 jakarta .servlet .FilterChain ;
9
+ import jakarta .servlet .ServletException ;
10
+ import jakarta .servlet .http .HttpServletRequest ;
11
+ import jakarta .servlet .http .HttpServletResponse ;
8
12
import lombok .extern .slf4j .Slf4j ;
9
13
import org .apache .commons .io .IOUtils ;
14
+ import org .springframework .security .authentication .AuthenticationDetailsSource ;
10
15
import org .springframework .security .authentication .AuthenticationManager ;
16
+ import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
11
17
import org .springframework .security .core .context .SecurityContextHolder ;
18
+ import org .springframework .security .web .authentication .www .BasicAuthenticationConverter ;
12
19
import org .springframework .security .web .authentication .www .BasicAuthenticationFilter ;
20
+ import org .springframework .util .Assert ;
13
21
import org .springframework .util .StringUtils ;
14
22
15
- import jakarta .servlet .FilterChain ;
16
- import jakarta .servlet .ServletException ;
17
- import jakarta .servlet .http .HttpServletRequest ;
18
- import jakarta .servlet .http .HttpServletResponse ;
19
23
import java .io .IOException ;
24
+ import java .nio .charset .Charset ;
20
25
import java .nio .charset .StandardCharsets ;
21
26
22
27
@ Slf4j
@@ -26,15 +31,33 @@ public class MyAuthenticationFilter extends BasicAuthenticationFilter {
26
31
27
32
private final Jwt jwt ;
28
33
34
+ private final BasicAuthenticationConverter authenticationConverter = new BasicAuthenticationConverter ();
35
+
29
36
public MyAuthenticationFilter (AuthenticationManager authenticationManager , Jwt jwt ) {
30
37
super (authenticationManager );
31
38
this .jwt = jwt ;
32
39
}
33
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
+
34
50
@ Override
35
51
protected void doFilterInternal (HttpServletRequest request , HttpServletResponse response , FilterChain chain ) throws IOException , ServletException {
36
52
log .debug ("token authentication ~" );
37
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 ;
59
+ }
60
+
38
61
String sign = request .getHeader (TOKEN_KEY );
39
62
if (!StringUtils .hasLength (sign )) {
40
63
writeResponse (response , Response .buildFailure ("token.error" , "token must not null." ));
0 commit comments