Skip to content

Commit 5e96e13

Browse files
committed
JWT authentication refactoring
1 parent 8ea5449 commit 5e96e13

File tree

11 files changed

+322
-31
lines changed

11 files changed

+322
-31
lines changed

authentication/authentication-commons-jwt/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<parent>
6-
<artifactId>stateless-auth</artifactId>
6+
<artifactId>authentication</artifactId>
77
<groupId>com.fd</groupId>
88
<version>1.0-SNAPSHOT</version>
99
</parent>

authentication/authentication-commons-jwt/src/main/java/com/fd/tryout/security/jwt/JwtUsernamePasswordAuthenticationFilter.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import io.jsonwebtoken.Jwts;
55
import io.jsonwebtoken.SignatureAlgorithm;
6+
import lombok.Data;
67
import lombok.var;
78
import org.springframework.security.authentication.AuthenticationManager;
89
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -52,6 +53,11 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
5253
@Override
5354
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
5455
FilterChain chain, Authentication auth) {
56+
String token = this.generateToken(auth);
57+
response.addHeader(config.getHeader(), config.getPrefix() + " " + token);
58+
}
59+
60+
private String generateToken(Authentication auth) {
5561
Instant now = Instant.now();
5662
List<String> authorities = auth.getAuthorities().stream().map(GrantedAuthority::getAuthority)
5763
.collect(Collectors.toList());
@@ -62,6 +68,12 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
6268
.setExpiration(Date.from(now.plusSeconds(config.getExpiration())))
6369
.signWith(SignatureAlgorithm.HS256, config.getSecret().getBytes())
6470
.compact();
65-
response.addHeader(config.getHeader(), config.getPrefix() + " " + token);
71+
return token;
72+
}
73+
74+
@Data
75+
private class User {
76+
private String username;
77+
private String password;
6678
}
6779
}

authentication/authentication-commons-jwt/src/main/java/com/fd/tryout/security/jwt/User.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

authentication/authentication-commons/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<parent>
6-
<artifactId>stateless-auth</artifactId>
6+
<artifactId>authentication</artifactId>
77
<groupId>com.fd</groupId>
88
<version>1.0-SNAPSHOT</version>
99
</parent>

authentication/authentication-db/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<parent>
6-
<artifactId>stateless-auth</artifactId>
6+
<artifactId>authentication</artifactId>
77
<groupId>com.fd</groupId>
88
<version>1.0-SNAPSHOT</version>
99
</parent>

authentication/jwt-authentication-filter.graphml

Lines changed: 300 additions & 0 deletions
Large diffs are not rendered by default.

authentication/jwt-authentication-service/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<parent>
6-
<artifactId>stateless-auth</artifactId>
6+
<artifactId>authentication</artifactId>
77
<groupId>com.fd</groupId>
88
<version>1.0-SNAPSHOT</version>
99
</parent>

authentication/jwt-authentication-service/src/main/java/com/fd/tryout/security/jwt/config/SecurityConfig.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ public AuthenticationProvider authenticationProvider() {
4040
return new DbAuthenticationProvider(this.userRepository);
4141
}
4242

43-
// @Autowired
44-
// public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
45-
// UserDetails admin = User.withUsername("admin").password("{noop}admin").roles("ADMIN, USER").build();
46-
// UserDetails john = User.withUsername("john").password("{noop}doe").roles("ADMIN, USER").build();
47-
//
48-
// auth.inMemoryAuthentication().withUser(admin).withUser(john);
49-
// }
50-
5143
@Override
5244
protected void configure(HttpSecurity httpSecurity) throws Exception {
5345
httpSecurity

authentication/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>com.fd</groupId>
8-
<artifactId>stateless-auth</artifactId>
8+
<artifactId>authentication</artifactId>
99
<parent>
1010
<groupId>org.springframework.boot</groupId>
1111
<artifactId>spring-boot-starter-parent</artifactId>
@@ -35,11 +35,11 @@
3535
<mapstruct.version>1.2.0.Final</mapstruct.version>
3636
<hibernate.jpamodelgen-version>5.3.2.Final</hibernate.jpamodelgen-version>
3737
<jaxb.version>2.3.0</jaxb.version>
38+
<jjwt.version>0.9.1</jjwt.version>
3839

3940
<maven.plugin.compiler.version>3.7.0</maven.plugin.compiler.version>
4041
<maven.plugin.build-helper.version>3.0.0</maven.plugin.build-helper.version>
4142
<maven.plugin.processor.version>3.2.0</maven.plugin.processor.version>
42-
4343
</properties>
4444

4545
<dependencyManagement>
@@ -106,7 +106,7 @@
106106
<dependency>
107107
<groupId>io.jsonwebtoken</groupId>
108108
<artifactId>jjwt</artifactId>
109-
<version>0.9.1</version>
109+
<version>${jjwt.version}</version>
110110
</dependency>
111111

112112
<dependency>

authentication/simple-authentication-service/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<parent>
6-
<artifactId>stateless-auth</artifactId>
6+
<artifactId>authentication</artifactId>
77
<groupId>com.fd</groupId>
88
<version>1.0-SNAPSHOT</version>
99
</parent>

0 commit comments

Comments
 (0)