17
17
18
18
import static org .springframework .http .HttpHeaders .AUTHORIZATION ;
19
19
20
- import java .io . UnsupportedEncodingException ;
20
+ import java .nio . charset . Charset ;
21
21
import java .nio .charset .StandardCharsets ;
22
22
import java .util .Base64 ;
23
23
24
24
import javax .servlet .http .HttpServletRequest ;
25
25
26
26
import org .springframework .security .authentication .AuthenticationDetailsSource ;
27
27
import org .springframework .security .authentication .BadCredentialsException ;
28
- import org .springframework .security .authentication .InternalAuthenticationServiceException ;
29
28
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
30
29
import org .springframework .security .web .authentication .AuthenticationConverter ;
31
30
import org .springframework .security .web .authentication .WebAuthenticationDetailsSource ;
@@ -47,7 +46,7 @@ public class BasicAuthenticationConverter implements AuthenticationConverter {
47
46
48
47
private AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource ;
49
48
50
- private String credentialsCharset = StandardCharsets .UTF_8 . name () ;
49
+ private Charset credentialsCharset = StandardCharsets .UTF_8 ;
51
50
52
51
public BasicAuthenticationConverter () {
53
52
this (new WebAuthenticationDetailsSource ());
@@ -58,16 +57,16 @@ public BasicAuthenticationConverter(
58
57
this .authenticationDetailsSource = authenticationDetailsSource ;
59
58
}
60
59
61
- public String getCredentialsCharset () {
62
- return credentialsCharset ;
60
+ public Charset getCredentialsCharset () {
61
+ return this . credentialsCharset ;
63
62
}
64
63
65
- public void setCredentialsCharset (String credentialsCharset ) {
64
+ public void setCredentialsCharset (Charset credentialsCharset ) {
66
65
this .credentialsCharset = credentialsCharset ;
67
66
}
68
67
69
68
public AuthenticationDetailsSource <HttpServletRequest , ?> getAuthenticationDetailsSource () {
70
- return authenticationDetailsSource ;
69
+ return this . authenticationDetailsSource ;
71
70
}
72
71
73
72
public void setAuthenticationDetailsSource (
@@ -88,34 +87,29 @@ public UsernamePasswordAuthenticationToken convert(HttpServletRequest request) {
88
87
return null ;
89
88
}
90
89
91
- byte [] base64Token = header .substring (6 ).getBytes ();
90
+ byte [] base64Token = header .substring (6 ).getBytes (StandardCharsets . UTF_8 );
92
91
byte [] decoded ;
93
92
try {
94
93
decoded = Base64 .getDecoder ().decode (base64Token );
95
- } catch (IllegalArgumentException e ) {
96
- throw new BadCredentialsException ("Failed to decode basic authentication token" );
97
94
}
98
-
99
- String token ;
100
- try {
101
- token = new String (decoded , getCredentialsCharset (request ));
102
- } catch (UnsupportedEncodingException e ) {
103
- throw new InternalAuthenticationServiceException (e .getMessage (), e );
95
+ catch (IllegalArgumentException e ) {
96
+ throw new BadCredentialsException (
97
+ "Failed to decode basic authentication token" );
104
98
}
105
99
106
- String [] tokens = token .split (":" );
107
- if (tokens .length != 2 ) {
108
- throw new BadCredentialsException ("Invalid basic authentication token" );
109
- }
100
+ String token = new String (decoded , getCredentialsCharset (request ));
110
101
111
- UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken (tokens [0 ],
112
- tokens [1 ]);
113
- authentication .setDetails (authenticationDetailsSource .buildDetails (request ));
102
+ int delim = token .indexOf (":" );
114
103
115
- return authentication ;
104
+ if (delim == -1 ) {
105
+ throw new BadCredentialsException ("Invalid basic authentication token" );
106
+ }
107
+ UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken (token .substring (0 , delim ), token .substring (delim + 1 ));
108
+ result .setDetails (this .authenticationDetailsSource .buildDetails (request ));
109
+ return result ;
116
110
}
117
111
118
- protected String getCredentialsCharset (HttpServletRequest request ) {
112
+ protected Charset getCredentialsCharset (HttpServletRequest request ) {
119
113
return getCredentialsCharset ();
120
114
}
121
115
0 commit comments