-
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): complete authentication flow
- Loading branch information
Johnny Miller (锺俊)
committed
Dec 21, 2020
1 parent
c2c4721
commit b0a91e0
Showing
11 changed files
with
177 additions
and
53 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
.../java/com/jmsoftware/maf/authcenter/universal/configuration/WebSecurityConfiguration.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,40 @@ | ||
package com.jmsoftware.maf.authcenter.universal.configuration; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
/** | ||
* <h1>WebSecurityConfiguration</h1> | ||
* <p> | ||
* Security handler configuration. | ||
* | ||
* @author Johnny Miller (锺俊), email: [email protected] | ||
* @date 5/2/20 11:41 PM | ||
**/ | ||
@Slf4j | ||
@Configuration | ||
@EnableWebSecurity | ||
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { | ||
@Bean | ||
public BCryptPasswordEncoder encoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
@Override | ||
public AuthenticationManager authenticationManager() throws Exception { | ||
return super.authenticationManagerBean(); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
// Disable Web Security. | ||
http.authorizeRequests().anyRequest().permitAll().and().csrf().disable(); | ||
} | ||
} |
32 changes: 30 additions & 2 deletions
32
auth-center/src/test/java/com/jmsoftware/maf/authcenter/AuthCenterApplicationTests.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 |
---|---|---|
@@ -1,13 +1,41 @@ | ||
package com.jmsoftware.maf.authcenter; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
import com.jmsoftware.maf.authcenter.universal.domain.UserPO; | ||
import com.jmsoftware.maf.authcenter.universal.domain.UserPrincipal; | ||
import com.jmsoftware.maf.authcenter.universal.service.JwtService; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Description: AuthCenterApplicationTests. | ||
* | ||
* @author 钟俊 (zhongjun), email: [email protected], date: 12/21/2020 3:08 PM | ||
*/ | ||
@Slf4j | ||
@SpringBootTest | ||
class AuthCenterApplicationTests { | ||
@Autowired | ||
private JwtService jwtService; | ||
|
||
@Test | ||
void contextLoads() { | ||
@SneakyThrows | ||
void mockLogin() { | ||
UserPO userPO = new UserPO(); | ||
userPO.setId(1L); | ||
userPO.setUsername("ijohnnymiller"); | ||
val authenticationToken = new UsernamePasswordAuthenticationToken( | ||
UserPrincipal.create(userPO, new ArrayList<>(), new ArrayList<>()), 12345678); | ||
String jwt = jwtService.createJwt(authenticationToken, false); | ||
log.info("Generated JWT: {}", jwt); | ||
Assertions.assertTrue(StrUtil.isNotBlank(jwt)); | ||
} | ||
|
||
} |
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
51 changes: 29 additions & 22 deletions
51
...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 |
---|---|---|
@@ -1,50 +1,57 @@ | ||
package com.jmsoftware.maf.gateway.universal.configuration; | ||
|
||
import com.google.common.collect.Lists; | ||
import cn.hutool.core.util.StrUtil; | ||
import com.jmsoftware.maf.common.bean.ResponseBodyBean; | ||
import com.jmsoftware.maf.common.domain.authcenter.user.GetUserByLoginTokenResponse; | ||
import com.jmsoftware.maf.common.exception.BusinessException; | ||
import com.jmsoftware.maf.gateway.remoteapi.AuthCenterRemoteApi; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.context.annotation.Lazy; | ||
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; | ||
import javax.annotation.Resource; | ||
|
||
/** | ||
* Description: AuthenticationManager, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: [email protected], date: 12/18/2020 3:40 PM | ||
**/ | ||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class AuthenticationManager implements ReactiveAuthenticationManager { | ||
private final JwtService jwtService; | ||
@Lazy | ||
@Resource | ||
private AuthCenterRemoteApi authCenterRemoteApi; | ||
|
||
@Override | ||
public Mono<Authentication> authenticate(Authentication authentication) { | ||
String authToken = authentication.getCredentials().toString(); | ||
val jwt = authentication.getCredentials().toString(); | ||
String username; | ||
try { | ||
username = jwtService.getUsernameFromJwt(authToken); | ||
username = jwtService.getUsernameFromJwt(jwt); | ||
} catch (Exception e) { | ||
username = null; | ||
log.error("Exception occurred when authenticating", e); | ||
return Mono.empty(); | ||
} | ||
// 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); | ||
if (StrUtil.isBlank(username)) { | ||
log.warn("Authentication failed! Cause: the username mustn't be blank"); | ||
return Mono.empty(); | ||
} | ||
val response = authCenterRemoteApi.getUserByLoginToken(username); | ||
Mono<GetUserByLoginTokenResponse> responseMono = response.map(ResponseBodyBean::getData) | ||
.switchIfEmpty(Mono.error(new BusinessException("Authentication failed! Cause: User not found"))); | ||
return responseMono.map(getUserByLoginTokenResponse -> { | ||
log.info("Authentication success. Username: {}", getUserByLoginTokenResponse.getUsername()); | ||
UserPrincipal userPrincipal = UserPrincipal.create(getUserByLoginTokenResponse, null, null); | ||
return new UsernamePasswordAuthenticationToken(userPrincipal, null); | ||
}); | ||
} | ||
} |
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
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
40 changes: 40 additions & 0 deletions
40
.../com/jmsoftware/maf/gateway/universal/configuration/RbacReactiveAuthorizationManager.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,40 @@ | ||
package com.jmsoftware.maf.gateway.universal.configuration; | ||
|
||
import com.jmsoftware.maf.gateway.remoteapi.AuthCenterRemoteApi; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.security.authorization.AuthorizationDecision; | ||
import org.springframework.security.authorization.ReactiveAuthorizationManager; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.server.authorization.AuthorizationContext; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
|
||
import javax.annotation.Resource; | ||
|
||
/** | ||
* Description: ReactiveAuthorizationManagerImpl, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: [email protected], date: 12/21/2020 12:38 PM | ||
**/ | ||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class RbacReactiveAuthorizationManager implements ReactiveAuthorizationManager<AuthorizationContext> { | ||
@Lazy | ||
@Resource | ||
private AuthCenterRemoteApi authCenterRemoteApi; | ||
|
||
@Override | ||
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, AuthorizationContext object) { | ||
return authentication.map(auth -> { | ||
ServerHttpRequest request = object.getExchange().getRequest(); | ||
UserPrincipal userPrincipal = (UserPrincipal) auth.getPrincipal(); | ||
log.info("Checking authorization for user: {}, resource: [{}] {}", userPrincipal.getUsername(), | ||
request.getMethod(), request.getURI()); | ||
return new AuthorizationDecision(true); | ||
}).defaultIfEmpty(new AuthorizationDecision(false)); | ||
} | ||
} |
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
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
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
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