Skip to content

Commit d12f537

Browse files
committed
fix security bug
1 parent 103f160 commit d12f537

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.codingapi.springboot</groupId>
1414
<artifactId>springboot-parent</artifactId>
15-
<version>2.0.0</version>
15+
<version>2.0.1</version>
1616

1717
<url>https://github.com/codingapi/springboot-framewrok</url>
1818
<name>springboot-parent</name>

springboot-starter-data-fast/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.0.0</version>
8+
<version>2.0.1</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-id-generator/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.0.0</version>
8+
<version>2.0.1</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-security-jwt/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.0.0</version>
9+
<version>2.0.1</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security-jwt</artifactId>

springboot-starter-security-jwt/src/main/java/com/codingapi/springboot/security/AutoConfiguration.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,18 @@ public PasswordEncoder passwordEncoder() {
5858
@ConditionalOnMissingBean
5959
public SecurityLoginHandler securityLoginHandler(){
6060
return (request, response, handler) -> {
61-
6261
};
6362
}
6463

64+
6565
@Bean
6666
@ConditionalOnMissingBean
6767
public SecurityFilterChain filterChain(HttpSecurity security, Jwt jwt,SecurityLoginHandler loginHandler,
6868
SecurityJwtProperties properties) throws Exception {
69-
//before add addCorsMappings to enable cors.
69+
//disable basic auth
7070
security.httpBasic().disable();
71+
72+
//before add addCorsMappings to enable cors.
7173
security.cors();
7274
if(properties.isDisableCsrf() ){
7375
security.csrf().disable();
@@ -78,9 +80,12 @@ public SecurityFilterChain filterChain(HttpSecurity security, Jwt jwt,SecurityLo
7880
.authenticationEntryPoint(new MyUnAuthenticationEntryPoint())
7981
.accessDeniedHandler(new MyAccessDeniedHandler())
8082
.and()
81-
.authorizeHttpRequests()
82-
.requestMatchers(properties.getAuthenticatedUrls()).authenticated()
83-
.and()
83+
.authorizeHttpRequests(
84+
registry -> {
85+
registry.requestMatchers(properties.getAuthenticatedUrls()).authenticated()
86+
.anyRequest().permitAll();
87+
}
88+
)
8489
//default login url :/login
8590
.formLogin()
8691
.loginProcessingUrl(properties.getLoginProcessingUrl())
@@ -90,8 +95,7 @@ public SecurityFilterChain filterChain(HttpSecurity security, Jwt jwt,SecurityLo
9095
.logout()
9196
.logoutUrl(properties.getLogoutUrl())
9297
.addLogoutHandler(new MyLogoutHandler())
93-
.logoutSuccessHandler(new MyLogoutSuccessHandler())
94-
.permitAll();
98+
.logoutSuccessHandler(new MyLogoutSuccessHandler());
9599

96100
return security.build();
97101
}

springboot-starter-security-jwt/src/main/java/com/codingapi/springboot/security/filter/MyAuthenticationFilter.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
import com.codingapi.springboot.security.exception.TokenExpiredException;
66
import com.codingapi.springboot.security.jwt.Jwt;
77
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;
812
import lombok.extern.slf4j.Slf4j;
913
import org.apache.commons.io.IOUtils;
14+
import org.springframework.security.authentication.AuthenticationDetailsSource;
1015
import org.springframework.security.authentication.AuthenticationManager;
16+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1117
import org.springframework.security.core.context.SecurityContextHolder;
18+
import org.springframework.security.web.authentication.www.BasicAuthenticationConverter;
1219
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
20+
import org.springframework.util.Assert;
1321
import 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;
1923
import java.io.IOException;
24+
import java.nio.charset.Charset;
2025
import 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."));

springboot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>2.0.0</version>
8+
<version>2.0.1</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

0 commit comments

Comments
 (0)