-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($gateway): integrate Spring Security
- Loading branch information
Johnny Miller (锺俊)
committed
Dec 18, 2020
1 parent
2e29759
commit f092c6b
Showing
16 changed files
with
990 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...c/main/java/com/jmsoftware/maf/gateway/universal/configuration/AuthenticationManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.jmsoftware.maf.gateway.universal.configuration; | ||
|
||
import com.google.common.collect.Lists; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.ReactiveAuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Description: AuthenticationManager, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/18/2020 3:40 PM | ||
**/ | ||
@Component | ||
@RequiredArgsConstructor | ||
public class AuthenticationManager implements ReactiveAuthenticationManager { | ||
private final JwtService jwtService; | ||
|
||
@Override | ||
public Mono<Authentication> authenticate(Authentication authentication) { | ||
String authToken = authentication.getCredentials().toString(); | ||
String username; | ||
try { | ||
username = jwtService.getUsernameFromJwt(authToken); | ||
} catch (Exception e) { | ||
username = null; | ||
} | ||
// if (username != null && !tokenProvider.isTokenExpired(authToken)) { | ||
// Claims claims = tokenProvider.getAllClaimsFromToken(authToken); | ||
// List roles = claims.get(AUTHORITIES_KEY, List.class); | ||
// List authorities = roles.stream().map(role -> new SimpleGrantedAuthority(role)).collect( | ||
// Collectors.toList()); | ||
// UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(username, username, | ||
// authorities); | ||
// SecurityContextHolder.getContext().setAuthentication(new UserPrincipal(username, authorities)); | ||
// return Mono.just(auth); | ||
// } else { | ||
// return Mono.empty(); | ||
// } | ||
List<GrantedAuthority> authorities = Lists.newLinkedList(); | ||
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(username, username, | ||
authorities); | ||
return Mono.just(auth); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.jmsoftware.maf.gateway.universal.configuration; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* <h1>Constants</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com | ||
* @date 5/2/20 11:41 PM | ||
**/ | ||
@Slf4j | ||
@Component | ||
public class Constants { | ||
public Constants(ProjectProperty projectProperty) { | ||
REDIS_JWT_KEY_PREFIX = String.format("%s:jwt:", projectProperty.getProjectParentArtifactId()); | ||
log.warn("Initiated 'REDIS_JWT_KEY_PREFIX': {}", REDIS_JWT_KEY_PREFIX); | ||
} | ||
|
||
/** | ||
* Key prefix of JWT stored in Redis. | ||
*/ | ||
public static String REDIS_JWT_KEY_PREFIX; | ||
/** | ||
* Token key of request header. | ||
*/ | ||
public static final String REQUEST_TOKEN_KEY = "Authorization"; | ||
/** | ||
* Prefix of JWT. | ||
*/ | ||
public static final String JWT_PREFIX = "Bearer "; | ||
/** | ||
* Star sign | ||
*/ | ||
public static final String ASTERISK = "*"; | ||
/** | ||
* At sign | ||
*/ | ||
public static final String AT_SIGN = "@"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...jmsoftware/maf/gateway/universal/configuration/CustomServerSecurityContextRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.jmsoftware.maf.gateway.universal.configuration; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.security.authentication.ReactiveAuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextImpl; | ||
import org.springframework.security.web.server.context.ServerSecurityContextRepository; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Description: SecurityContextRepository, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/18/2020 3:38 PM | ||
**/ | ||
@Slf4j | ||
@Component | ||
public class CustomServerSecurityContextRepository implements ServerSecurityContextRepository { | ||
private static final String TOKEN_PREFIX = "Bearer "; | ||
|
||
@Autowired | ||
private ReactiveAuthenticationManager authenticationManager; | ||
|
||
@Override | ||
public Mono save(ServerWebExchange swe, SecurityContext sc) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public Mono load(ServerWebExchange swe) { | ||
ServerHttpRequest request = swe.getRequest(); | ||
String authHeader = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION); | ||
String authToken = null; | ||
if (authHeader != null && authHeader.startsWith(TOKEN_PREFIX)) { | ||
authToken = authHeader.replace(TOKEN_PREFIX, ""); | ||
}else { | ||
log.warn("couldn't find bearer string, will ignore the header."); | ||
} | ||
if (authToken != null) { | ||
Authentication auth = new UsernamePasswordAuthenticationToken(authToken, authToken); | ||
return this.authenticationManager.authenticate(auth).map((authentication) -> new SecurityContextImpl(authentication)); | ||
} else { | ||
return Mono.empty(); | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/IgnoredRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.jmsoftware.maf.gateway.universal.configuration; | ||
|
||
import com.google.common.collect.Lists; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* <h1>IgnoredRequest</h1> | ||
* <p> | ||
* Ignored request configuration. | ||
* | ||
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com | ||
* @date 5/2/20 11:41 PM | ||
**/ | ||
@Data | ||
public class IgnoredRequest { | ||
/** | ||
* Ignored URL pattern. | ||
*/ | ||
private List<String> pattern = Lists.newArrayList(); | ||
/** | ||
* Ignored GET request. | ||
*/ | ||
private List<String> get = Lists.newArrayList(); | ||
/** | ||
* Ignored POST request. | ||
*/ | ||
private List<String> post = Lists.newArrayList(); | ||
/** | ||
* Ignored DELETE request. | ||
*/ | ||
private List<String> delete = Lists.newArrayList(); | ||
/** | ||
* Ignored PUT request. | ||
*/ | ||
private List<String> put = Lists.newArrayList(); | ||
/** | ||
* Ignored HEAD request. | ||
*/ | ||
private List<String> head = Lists.newArrayList(); | ||
/** | ||
* Ignored PATCH request. | ||
*/ | ||
private List<String> patch = Lists.newArrayList(); | ||
/** | ||
* Ignored OPTIONS request. | ||
*/ | ||
private List<String> options = Lists.newArrayList(); | ||
/** | ||
* Ignored TRACE request. | ||
*/ | ||
private List<String> trace = Lists.newArrayList(); | ||
} |
41 changes: 41 additions & 0 deletions
41
...ay/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/JwtConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.jmsoftware.maf.gateway.universal.configuration; | ||
|
||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* <h1>JwtConfiguration</h1> | ||
* <p> | ||
* Ignored request configuration. | ||
* | ||
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com | ||
* @date 5/2/20 11:41 PM | ||
**/ | ||
@Data | ||
@Slf4j | ||
@Component | ||
@ConfigurationProperties(prefix = "jwt.configuration") | ||
public class JwtConfiguration { | ||
public JwtConfiguration(ProjectProperty projectProperty) { | ||
this.signingKey = String.format("%s %s", projectProperty.getProjectParentArtifactId(), projectProperty.getVersion()); | ||
log.info("Initiated JWT signing key: {}. The specified key byte array is {} bits", this.signingKey, | ||
this.signingKey.getBytes(StandardCharsets.UTF_8).length * 8); | ||
} | ||
|
||
/** | ||
* JWT signing key, which is equal to the string value of group id of project. | ||
*/ | ||
private String signingKey; | ||
/** | ||
* Time to live of JWT. Default: 3 * 600000 milliseconds (30 min). | ||
*/ | ||
private Long ttl = 3 * 600000L; | ||
/** | ||
* Time to live of JWT for remember me, Default: 7 * 86400000 milliseconds (7 day) | ||
*/ | ||
private Long ttlForRememberMe = 7 * 86400000L; | ||
} |
Oops, something went wrong.