Update version and change formating
This commit is contained in:
10
pom.xml
10
pom.xml
@@ -14,7 +14,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.6.RELEASE</version>
|
||||
<version>2.5.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>11</java.version>
|
||||
<io.jsonwebtoken.version>0.11.2</io.jsonwebtoken.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -40,18 +41,18 @@
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-api</artifactId>
|
||||
<version>0.11.1</version>
|
||||
<version>${io.jsonwebtoken.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-impl</artifactId>
|
||||
<version>0.11.1</version>
|
||||
<version>${io.jsonwebtoken.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-jackson</artifactId>
|
||||
<version>0.11.1</version>
|
||||
<version>${io.jsonwebtoken.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -87,5 +88,4 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
||||
@@ -11,7 +11,5 @@ import lombok.ToString;
|
||||
*/
|
||||
@Data @NoArgsConstructor @AllArgsConstructor @ToString
|
||||
public class Message {
|
||||
|
||||
private String content;
|
||||
|
||||
}
|
||||
|
||||
@@ -21,24 +21,18 @@ import lombok.ToString;
|
||||
*/
|
||||
@ToString @AllArgsConstructor @NoArgsConstructor
|
||||
public class User implements UserDetails {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
@Getter @Setter
|
||||
private Boolean enabled;
|
||||
|
||||
@Getter @Setter
|
||||
private List<Role> roles;
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
@@ -73,7 +67,6 @@ public class User implements UserDetails {
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
@JsonProperty
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
|
||||
@@ -23,7 +23,6 @@ public class AuthenticationREST {
|
||||
|
||||
@Autowired
|
||||
private JWTUtil jwtUtil;
|
||||
|
||||
@Autowired
|
||||
private PBKDF2Encoder passwordEncoder;
|
||||
|
||||
|
||||
@@ -14,19 +14,16 @@ import reactor.core.publisher.Mono;
|
||||
*/
|
||||
@RestController
|
||||
public class ResourceREST {
|
||||
|
||||
@RequestMapping(value = "/resource/user", method = RequestMethod.GET)
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
public Mono<ResponseEntity<?>> user() {
|
||||
return Mono.just(ResponseEntity.ok(new Message("Content for user")));
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/resource/admin", method = RequestMethod.GET)
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public Mono<ResponseEntity<?>> admin() {
|
||||
return Mono.just(ResponseEntity.ok(new Message("Content for admin")));
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/resource/user-or-admin", method = RequestMethod.GET)
|
||||
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
|
||||
public Mono<ResponseEntity<?>> userOrAdmin() {
|
||||
|
||||
@@ -21,12 +21,10 @@ public class AuthenticationManager implements ReactiveAuthenticationManager {
|
||||
|
||||
@Autowired
|
||||
private JWTUtil jwtUtil;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Mono<Authentication> authenticate(Authentication authentication) {
|
||||
String authToken = authentication.getCredentials().toString();
|
||||
|
||||
try {
|
||||
String username = jwtUtil.getUsernameFromToken(authToken);
|
||||
if (!jwtUtil.validateToken(authToken)) {
|
||||
|
||||
@@ -21,10 +21,8 @@ import org.springframework.stereotype.Component;
|
||||
*/
|
||||
@Component
|
||||
public class JWTUtil {
|
||||
|
||||
@Value("${springbootwebfluxjjwt.jjwt.secret}")
|
||||
private String secret;
|
||||
|
||||
@Value("${springbootwebfluxjjwt.jjwt.expiration}")
|
||||
private String expirationTime;
|
||||
|
||||
@@ -38,20 +36,16 @@ public class JWTUtil {
|
||||
public Claims getAllClaimsFromToken(String token) {
|
||||
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
|
||||
}
|
||||
|
||||
public String getUsernameFromToken(String token) {
|
||||
return getAllClaimsFromToken(token).getSubject();
|
||||
}
|
||||
|
||||
public Date getExpirationDateFromToken(String token) {
|
||||
return getAllClaimsFromToken(token).getExpiration();
|
||||
}
|
||||
|
||||
private Boolean isTokenExpired(String token) {
|
||||
final Date expiration = getExpirationDateFromToken(token);
|
||||
return expiration.before(new Date());
|
||||
}
|
||||
|
||||
public String generateToken(User user) {
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
claims.put("role", user.getRoles());
|
||||
@@ -60,7 +54,6 @@ public class JWTUtil {
|
||||
|
||||
private String doGenerateToken(Map<String, Object> claims, String username) {
|
||||
Long expirationTimeLong = Long.parseLong(expirationTime); //in second
|
||||
|
||||
final Date createdDate = new Date();
|
||||
final Date expirationDate = new Date(createdDate.getTime() + expirationTimeLong * 1000);
|
||||
|
||||
@@ -72,7 +65,6 @@ public class JWTUtil {
|
||||
.signWith(key)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public Boolean validateToken(String token) {
|
||||
return !isTokenExpired(token);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ import org.springframework.stereotype.Component;
|
||||
*/
|
||||
@Component
|
||||
public class PBKDF2Encoder implements PasswordEncoder{
|
||||
|
||||
@Value("${springbootwebfluxjjwt.password.encoder.secret}")
|
||||
private String secret;
|
||||
|
||||
@@ -24,7 +23,6 @@ public class PBKDF2Encoder implements PasswordEncoder{
|
||||
|
||||
@Value("${springbootwebfluxjjwt.password.encoder.keylength}")
|
||||
private Integer keylength;
|
||||
|
||||
/**
|
||||
* More info (https://www.owasp.org/index.php/Hashing_Java) 404 :(
|
||||
* @param cs password
|
||||
@@ -46,5 +44,4 @@ public class PBKDF2Encoder implements PasswordEncoder{
|
||||
public boolean matches(CharSequence cs, String string) {
|
||||
return encode(cs).equals(string);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ import reactor.core.publisher.Mono;
|
||||
*/
|
||||
@Component
|
||||
public class SecurityContextRepository implements ServerSecurityContextRepository{
|
||||
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
@@ -42,5 +41,4 @@ public class SecurityContextRepository implements ServerSecurityContextRepositor
|
||||
return Mono.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ public class WebSecurityConfig {
|
||||
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
@Autowired
|
||||
private SecurityContextRepository securityContextRepository;
|
||||
|
||||
|
||||
@@ -11,9 +11,7 @@ import lombok.ToString;
|
||||
*/
|
||||
@Data @NoArgsConstructor @AllArgsConstructor @ToString
|
||||
public class AuthRequest {
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import lombok.ToString;
|
||||
*/
|
||||
@Data @NoArgsConstructor @AllArgsConstructor @ToString
|
||||
public class AuthResponse {
|
||||
|
||||
private String token;
|
||||
|
||||
}
|
||||
|
||||
@@ -17,22 +17,18 @@ import reactor.core.publisher.Mono;
|
||||
*/
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
// this is just an example, you can load the user from the database from the repository
|
||||
|
||||
private Map<String, User> data;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
data = new HashMap<>();
|
||||
|
||||
//username:passwowrd -> user:user
|
||||
data.put("user", new User("user", "cBrlgyL2GI2GINuLUUwgojITuIufFycpLG4490dhGtY=", true, Arrays.asList(Role.ROLE_USER)));
|
||||
|
||||
//username:passwowrd -> admin:admin
|
||||
data.put("admin", new User("admin", "dQNjUIMorJb8Ubj2+wVGYp6eAeYkdekqAcnYp+aRq5w=", true, Arrays.asList(Role.ROLE_ADMIN)));
|
||||
}
|
||||
|
||||
public Mono<User> findByUsername(String username) {
|
||||
if (data.containsKey(username)) {
|
||||
return Mono.just(data.get(username));
|
||||
@@ -40,5 +36,4 @@ public class UserService {
|
||||
return Mono.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
package id.web.ard.springbootwebfluxjjwt;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootWebfluxJjwtApplicationTests {
|
||||
class DemoApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user