Skip to content

Commit cb5fd7b

Browse files
committed
feature: add double token support for console and raft registry api
1 parent 5752786 commit cb5fd7b

31 files changed

+1381
-327
lines changed

common/src/main/java/org/apache/seata/common/ConfigurationKeys.java

+30
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public interface ConfigurationKeys {
6868
*/
6969
String SEATA_PREFIX = SEATA_FILE_ROOT_CONFIG + ".";
7070

71+
/**
72+
* The constant SECURITY_PREFIX
73+
*/
74+
String SECURITY_PREFIX = "security.";
75+
7176
/**
7277
* The constant SERVICE_PREFIX.
7378
*/
@@ -1014,6 +1019,31 @@ public interface ConfigurationKeys {
10141019
*/
10151020
String SERVER_APPLICATION_DATA_SIZE_CHECK = SERVER_PREFIX + "applicationDataLimitCheck";
10161021

1022+
/**
1023+
* The constant SECURITY_USERNAME;
1024+
*/
1025+
String SECURITY_USERNME = SECURITY_PREFIX + "username";
1026+
1027+
/**
1028+
* The constant SECURITY_PASSWORD;
1029+
*/
1030+
String SECURITY_PASSWORD = SECURITY_PREFIX + "password";
1031+
1032+
/**
1033+
* The constant SECURITY_SECRET_KEY;
1034+
*/
1035+
String SECURITY_SECRET_KEY = SECURITY_PREFIX + "secretKey";
1036+
1037+
/**
1038+
* The constant SECURITY_ACCESS_TOKEN_VALID_TIME;
1039+
*/
1040+
String SECURITY_ACCESS_TOKEN_VALID_TIME = SECURITY_PREFIX + "accessTokenValidityInMilliseconds";
1041+
1042+
/**
1043+
* The constant SECURITY_REFRESH_TOKEN_VALID_TIME;
1044+
*/
1045+
String SECURITY_REFRESH_TOKEN_VALID_TIME = SECURITY_PREFIX + "refreshTokenValidityInMilliseconds";
1046+
10171047
/**
10181048
* The constant ROCKET_MQ_MSG_TIMEOUT
10191049
*/

common/src/main/java/org/apache/seata/common/result/Code.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,49 @@
1616
*/
1717
package org.apache.seata.common.result;
1818

19-
19+
/**
20+
* The Code for the response of message
21+
*
22+
*/
2023
public enum Code {
2124
/**
2225
* response success
2326
*/
2427
SUCCESS("200", "ok"),
28+
/**
29+
* the custom error
30+
*/
31+
ACCESS_TOKEN_NEAR_EXPIRATION("200", "Access token is near expiration"),
2532
/**
2633
* server error
2734
*/
2835
ERROR("500", "Server error"),
2936
/**
3037
* the custom error
3138
*/
32-
LOGIN_FAILED("401", "Login failed");
39+
LOGIN_FAILED("401", "Login failed"),
40+
/**
41+
* the custom error
42+
*/
43+
CHECK_TOKEN_FAILED("401", "Check token failed"),
44+
/**
45+
* the custom error
46+
*/
47+
ACCESS_TOKEN_EXPIRED("401", "Access token expired"),
48+
/**
49+
* the custom error
50+
*/
51+
REFRESH_TOKEN_EXPIRED("401", "Refresh token expired");
3352

3453
/**
3554
* The Code.
3655
*/
37-
public String code;
56+
private String code;
3857

3958
/**
4059
* The Msg.
4160
*/
42-
public String msg;
61+
private String msg;
4362

4463
private Code(String code, String msg) {
4564
this.code = code;
@@ -98,4 +117,3 @@ public static String getErrorMsg(String code) {
98117
return null;
99118
}
100119
}
101-

common/src/main/java/org/apache/seata/common/result/SingleResult.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818

1919
import java.io.Serializable;
2020

21-
2221
/**
2322
* The single result
2423
*/
25-
public class SingleResult<T> extends Result<T> implements Serializable {
24+
public class SingleResult<T> extends Result<T> implements Serializable {
2625
private static final long serialVersionUID = 77612626624298767L;
2726

2827
/**
@@ -33,22 +32,18 @@ public class SingleResult<T> extends Result<T> implements Serializable {
3332
public SingleResult(String code, String message) {
3433
super(code, message);
3534
}
35+
public SingleResult(Code code) {
36+
super(code.getCode(), code.getMsg());
37+
}
3638

3739
public SingleResult(String code, String message, T data) {
3840
super(code, message);
3941
this.data = data;
4042
}
4143

42-
public static <T> SingleResult<T> failure(String code, String msg) {
43-
return new SingleResult<>(code, msg);
44-
}
45-
46-
public static <T> SingleResult<T> failure(Code errorCode) {
47-
return new SingleResult(errorCode.getCode(), errorCode.getMsg());
48-
}
49-
50-
public static <T> SingleResult<T> success(T data) {
51-
return new SingleResult<>(SUCCESS_CODE, SUCCESS_MSG,data);
44+
public SingleResult(Code code, T data) {
45+
super(code.getCode(), code.getMsg());
46+
this.data = data;
5247
}
5348

5449
public T getData() {

console/src/main/java/org/apache/seata/console/config/WebSecurityConfig.java renamed to console/src/main/java/org/apache/seata/console/config/ConsoleSecurityConfig.java

+23-11
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
package org.apache.seata.console.config;
1818

1919
import org.apache.seata.common.util.StringUtils;
20-
import org.apache.seata.console.filter.JwtAuthenticationTokenFilter;
20+
import org.apache.seata.console.filter.ConsoleAuthenticationTokenFilter;
2121
import org.apache.seata.console.security.CustomUserDetailsServiceImpl;
2222
import org.apache.seata.console.security.JwtAuthenticationEntryPoint;
2323
import org.apache.seata.console.utils.JwtTokenUtils;
2424
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.beans.factory.annotation.Qualifier;
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.core.annotation.Order;
2729
import org.springframework.core.env.Environment;
2830
import org.springframework.security.authentication.AuthenticationManager;
29-
import org.springframework.security.config.BeanIds;
3031
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
31-
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
3232
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3333
import org.springframework.security.config.annotation.web.builders.WebSecurity;
3434
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -44,19 +44,29 @@
4444
*
4545
*/
4646
@Configuration(proxyBeanMethods = false)
47-
@EnableGlobalMethodSecurity(prePostEnabled = true)
48-
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
47+
@Order(2)
48+
public class ConsoleSecurityConfig extends WebSecurityConfigurerAdapter {
4949

5050
/**
5151
* The constant AUTHORIZATION_HEADER.
5252
*/
5353
public static final String AUTHORIZATION_HEADER = "Authorization";
5454

55+
/**
56+
* The constant REFRESH_TOKEN.
57+
*/
58+
public static final String REFRESH_TOKEN = "refresh_token";
59+
5560
/**
5661
* The constant AUTHORIZATION_TOKEN.
5762
*/
5863
public static final String AUTHORIZATION_TOKEN = "access_token";
5964

65+
/**
66+
* The constant ACCESS_TOKEN_NEAR_EXPIRATION.
67+
*/
68+
public static final String ACCESS_TOKEN_NEAR_EXPIRATION = "Access_token_near_expiration";
69+
6070
/**
6171
* The constant SECURITY_IGNORE_URLS_SPILT_CHAR.
6272
*/
@@ -68,18 +78,21 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
6878
public static final String TOKEN_PREFIX = "Bearer ";
6979

7080
@Autowired
81+
@Qualifier("consoleUserDetailsService")
7182
private CustomUserDetailsServiceImpl userDetailsService;
7283

7384
@Autowired
85+
@Qualifier("consoleJwtAuthenticationEntryPoint")
7486
private JwtAuthenticationEntryPoint unauthorizedHandler;
7587

7688
@Autowired
89+
@Qualifier("consoleJwtTokenUtils")
7790
private JwtTokenUtils tokenProvider;
7891

7992
@Autowired
8093
private Environment env;
8194

82-
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
95+
@Bean("consoleAuthenticationManager")
8396
@Override
8497
public AuthenticationManager authenticationManagerBean() throws Exception {
8598
return super.authenticationManagerBean();
@@ -92,7 +105,7 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
92105

93106
@Override
94107
public void configure(WebSecurity web) {
95-
String ignoreURLs = env.getProperty("seata.security.ignore.urls", "/**");
108+
String ignoreURLs = env.getProperty("console.ignore.urls", "/**");
96109
for (String ignoreURL : ignoreURLs.trim().split(SECURITY_IGNORE_URLS_SPILT_CHAR)) {
97110
web.ignoring().antMatchers(ignoreURL.trim());
98111
}
@@ -110,9 +123,9 @@ protected void configure(HttpSecurity http) throws Exception {
110123
csrf.ignoringAntMatchers(csrfIgnoreUrls.trim().split(SECURITY_IGNORE_URLS_SPILT_CHAR));
111124
}
112125
csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
113-
// don't disable csrf, jwt may be implemented based on cookies
114-
http.addFilterBefore(new JwtAuthenticationTokenFilter(tokenProvider),
115-
UsernamePasswordAuthenticationFilter.class);
126+
// don't disable csrf, jwt may be implemented based on cookies
127+
http.antMatcher("/api/v1/**").addFilterBefore(new ConsoleAuthenticationTokenFilter(tokenProvider),
128+
UsernamePasswordAuthenticationFilter.class);
116129

117130
// disable cache
118131
http.headers().cacheControl();
@@ -123,7 +136,6 @@ protected void configure(HttpSecurity http) throws Exception {
123136
*
124137
* @return the password encoder
125138
*/
126-
@Bean
127139
public PasswordEncoder passwordEncoder() {
128140
return new BCryptPasswordEncoder();
129141
}

console/src/main/java/org/apache/seata/console/controller/AuthController.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
*/
1717
package org.apache.seata.console.controller;
1818

19-
import javax.servlet.http.HttpServletResponse;
20-
2119
import org.apache.seata.common.result.Code;
22-
import org.apache.seata.console.config.WebSecurityConfig;
2320
import org.apache.seata.common.result.SingleResult;
21+
import org.apache.seata.console.config.ConsoleSecurityConfig;
2422
import org.apache.seata.console.security.User;
2523
import org.apache.seata.console.utils.JwtTokenUtils;
2624
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.beans.factory.annotation.Qualifier;
2726
import org.springframework.security.authentication.AuthenticationManager;
2827
import org.springframework.security.authentication.BadCredentialsException;
2928
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -33,6 +32,7 @@
3332
import org.springframework.web.bind.annotation.RequestBody;
3433
import org.springframework.web.bind.annotation.RequestMapping;
3534
import org.springframework.web.bind.annotation.RestController;
35+
import javax.servlet.http.HttpServletResponse;
3636

3737
/**
3838
* auth user
@@ -42,8 +42,11 @@
4242
@RequestMapping("/api/v1/auth")
4343
public class AuthController {
4444
@Autowired
45+
@Qualifier("consoleJwtTokenUtils")
4546
private JwtTokenUtils jwtTokenUtils;
47+
4648
@Autowired
49+
@Qualifier("consoleAuthenticationManager")
4750
private AuthenticationManager authenticationManager;
4851

4952
/**
@@ -57,23 +60,24 @@ public class AuthController {
5760
@PostMapping("/login")
5861
public SingleResult<String> login(HttpServletResponse response, @RequestBody User user) {
5962
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
60-
user.getUsername(), user.getPassword());
63+
user.getUsername(), user.getPassword());
6164

6265
try {
6366
//AuthenticationManager(default ProviderManager) #authenticate check Authentication
6467
Authentication authentication = authenticationManager.authenticate(authenticationToken);
6568
//bind authentication to securityContext
6669
SecurityContextHolder.getContext().setAuthentication(authentication);
6770
//create token
68-
String token = jwtTokenUtils.createToken(authentication);
71+
String accessToken = jwtTokenUtils.createAccessToken(authentication);
72+
String refreshToken = jwtTokenUtils.createRefreshToken(authentication);
6973

70-
String authHeader = WebSecurityConfig.TOKEN_PREFIX + token;
74+
String authHeader = ConsoleSecurityConfig.TOKEN_PREFIX + accessToken;
7175
//put token into http header
72-
response.addHeader(WebSecurityConfig.AUTHORIZATION_HEADER, authHeader);
73-
74-
return SingleResult.success(authHeader);
76+
response.addHeader(ConsoleSecurityConfig.AUTHORIZATION_HEADER, authHeader);
77+
response.addHeader(ConsoleSecurityConfig.REFRESH_TOKEN, refreshToken);
78+
return new SingleResult<>(Code.SUCCESS, authHeader);
7579
} catch (BadCredentialsException authentication) {
76-
return SingleResult.failure(Code.LOGIN_FAILED);
80+
return new SingleResult<>(Code.LOGIN_FAILED);
7781
}
7882
}
7983
}

console/src/main/java/org/apache/seata/console/controller/OverviewController.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
*/
1717
package org.apache.seata.console.controller;
1818

19-
import java.util.ArrayList;
20-
import java.util.HashMap;
21-
import java.util.List;
22-
import java.util.Map;
23-
24-
19+
import org.apache.seata.common.result.Code;
2520
import org.apache.seata.common.result.SingleResult;
2621
import org.springframework.web.bind.annotation.GetMapping;
2722
import org.springframework.web.bind.annotation.RequestMapping;
2823
import org.springframework.web.bind.annotation.RestController;
24+
import java.util.ArrayList;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
2928

3029
/**
3130
* Overview
@@ -50,7 +49,6 @@ public SingleResult<List> getData() {
5049
hashMap.put("id", count);
5150
result.add(hashMap);
5251
}
53-
54-
return SingleResult.success(result);
52+
return new SingleResult<>(Code.SUCCESS, result);
5553
}
5654
}

0 commit comments

Comments
 (0)