mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-05-15 04:52:09 +08:00
CongressService & MomentaryService
This commit is contained in:
@@ -30,6 +30,7 @@ public class LoginCredential implements Authentication {
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 3125709257481600320L;
|
||||
String congress;
|
||||
String username;
|
||||
String password;
|
||||
String sessionId;
|
||||
@@ -64,7 +65,15 @@ public class LoginCredential implements Authentication {
|
||||
this.authType = authType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCongress() {
|
||||
return congress;
|
||||
}
|
||||
|
||||
public void setCongress(String congress) {
|
||||
this.congress = congress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Login Credential";
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
package org.maxkey.authn.jwt;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -24,7 +25,10 @@ import org.maxkey.authn.SigninPrincipal;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
|
||||
public class AuthJwt {
|
||||
public class AuthJwt implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -914373258878811144L;
|
||||
|
||||
private String ticket;
|
||||
private String token;
|
||||
private String type = "Bearer";
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.maxkey.authn.SigninPrincipal;
|
||||
import org.maxkey.configuration.AuthJwkConfig;
|
||||
import org.maxkey.crypto.jwt.HMAC512Service;
|
||||
import org.maxkey.entity.UserInfo;
|
||||
import org.maxkey.web.WebContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -41,6 +42,8 @@ public class AuthJwtService {
|
||||
HMAC512Service hmac512Service;
|
||||
|
||||
AuthJwkConfig authJwkConfig;
|
||||
|
||||
CongressService congressService;
|
||||
|
||||
public AuthJwtService(AuthJwkConfig authJwkConfig) throws JOSEException {
|
||||
this.authJwkConfig = authJwkConfig;
|
||||
@@ -48,6 +51,17 @@ public class AuthJwtService {
|
||||
this.hmac512Service = new HMAC512Service(authJwkConfig.getSecret());
|
||||
}
|
||||
|
||||
public AuthJwtService(AuthJwkConfig authJwkConfig,CongressService congressService) throws JOSEException {
|
||||
this.authJwkConfig = authJwkConfig;
|
||||
|
||||
this.congressService = congressService;
|
||||
|
||||
this.hmac512Service = new HMAC512Service(authJwkConfig.getSecret());
|
||||
}
|
||||
public AuthJwt generateAuthJwt(Authentication authentication) {
|
||||
return new AuthJwt(generateToken(authentication), authentication);
|
||||
}
|
||||
|
||||
public String generateToken(Authentication authentication) {
|
||||
String token = "";
|
||||
SigninPrincipal principal = ((SigninPrincipal)authentication.getPrincipal());
|
||||
@@ -95,4 +109,20 @@ public class AuthJwtService {
|
||||
return claims.getJWTID();
|
||||
}
|
||||
|
||||
public String createCongress(Authentication authentication) {
|
||||
String congress = WebContext.genId();
|
||||
congressService.store(
|
||||
congress,
|
||||
new AuthJwt(
|
||||
generateToken(authentication),
|
||||
authentication)
|
||||
);
|
||||
return congress;
|
||||
}
|
||||
|
||||
public AuthJwt consumeCongress(String congress) {
|
||||
AuthJwt authJwt = congressService.consume(congress);
|
||||
return authJwt;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.maxkey.authn.jwt;
|
||||
|
||||
public interface CongressService {
|
||||
|
||||
public void store(String congress, AuthJwt authJwt);
|
||||
|
||||
public AuthJwt consume(String congress);
|
||||
|
||||
public AuthJwt remove(String congress);
|
||||
|
||||
public AuthJwt get(String congress);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.maxkey.authn.jwt;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
|
||||
public class InMemoryCongressService implements CongressService{
|
||||
private static final Logger _logger = LoggerFactory.getLogger(InMemoryCongressService.class);
|
||||
|
||||
protected static Cache<String, AuthJwt> congressStore =
|
||||
Caffeine.newBuilder()
|
||||
.expireAfterWrite(3, TimeUnit.MINUTES)
|
||||
.maximumSize(200000)
|
||||
.build();
|
||||
|
||||
public InMemoryCongressService() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void store(String congress, AuthJwt authJwt) {
|
||||
congressStore.put(congress, authJwt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthJwt remove(String congress) {
|
||||
AuthJwt authJwt = congressStore.getIfPresent(congress);
|
||||
congressStore.invalidate(congress);
|
||||
return authJwt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthJwt get(String congress) {
|
||||
AuthJwt authJwt = congressStore.getIfPresent(congress);
|
||||
return authJwt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthJwt consume(String congress) {
|
||||
AuthJwt authJwt = congressStore.getIfPresent(congress);
|
||||
congressStore.invalidate(congress);
|
||||
return authJwt;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.maxkey.authn.jwt;
|
||||
|
||||
import org.maxkey.persistence.redis.RedisConnection;
|
||||
import org.maxkey.persistence.redis.RedisConnectionFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public class RedisCongressService implements CongressService {
|
||||
private static final Logger _logger = LoggerFactory.getLogger(RedisCongressService.class);
|
||||
|
||||
protected int validitySeconds = 60 * 3; //default 3 minutes.
|
||||
|
||||
RedisConnectionFactory connectionFactory;
|
||||
|
||||
public static String PREFIX="REDIS_CONGRESS_";
|
||||
/**
|
||||
* @param connectionFactory
|
||||
*/
|
||||
public RedisCongressService(
|
||||
RedisConnectionFactory connectionFactory) {
|
||||
super();
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public RedisCongressService() {
|
||||
|
||||
}
|
||||
|
||||
public void setConnectionFactory(RedisConnectionFactory connectionFactory) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void store(String congress, AuthJwt authJwt) {
|
||||
RedisConnection conn = connectionFactory.getConnection();
|
||||
conn.setexObject(PREFIX + congress, validitySeconds, authJwt);
|
||||
conn.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthJwt remove(String congress) {
|
||||
RedisConnection conn=connectionFactory.getConnection();
|
||||
AuthJwt authJwt = conn.getObject(PREFIX + congress);
|
||||
conn.delete(PREFIX+congress);
|
||||
conn.close();
|
||||
return authJwt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthJwt get(String congress) {
|
||||
RedisConnection conn = connectionFactory.getConnection();
|
||||
AuthJwt authJwt = conn.getObject(PREFIX + congress);
|
||||
conn.close();
|
||||
return authJwt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthJwt consume(String congress) {
|
||||
RedisConnection conn=connectionFactory.getConnection();
|
||||
AuthJwt authJwt = conn.getObject(PREFIX + congress);
|
||||
conn.delete(PREFIX+congress);
|
||||
conn.close();
|
||||
return authJwt;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -30,11 +30,14 @@ import org.maxkey.entity.UserInfo;
|
||||
import org.maxkey.util.AuthorizationHeaderUtils;
|
||||
import org.maxkey.web.WebConstants;
|
||||
import org.maxkey.web.WebContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
public class AuthorizationUtils {
|
||||
|
||||
static final String Authorization_Cookie = "AuthJWT";
|
||||
private static final Logger _logger = LoggerFactory.getLogger(AuthorizationUtils.class);
|
||||
|
||||
public static final String Authorization_Cookie = "congress";
|
||||
|
||||
public static void authenticateWithCookie(
|
||||
HttpServletRequest request,
|
||||
@@ -46,6 +49,7 @@ public class AuthorizationUtils {
|
||||
if(authCookie != null ) {
|
||||
String authorization = authCookie.getValue();
|
||||
doJwtAuthenticate(authorization,authJwtService,onlineTicketService);
|
||||
_logger.debug("congress automatic authenticated .");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,6 +63,7 @@ public class AuthorizationUtils {
|
||||
String authorization = AuthorizationHeaderUtils.resolveBearer(request);
|
||||
if(authorization != null ) {
|
||||
doJwtAuthenticate(authorization,authJwtService,onlineTicketService);
|
||||
_logger.debug("Authorization automatic authenticated .");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class PermissionInterceptor implements AsyncHandlerInterceptor {
|
||||
*/
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
|
||||
_logger.trace("PermissionAdapter preHandle");
|
||||
_logger.trace("Permission Interceptor .");
|
||||
AuthorizationUtils.authenticate(request, authJwtService, onlineTicketService);
|
||||
SigninPrincipal principal = AuthorizationUtils.getPrincipal();
|
||||
//判断用户是否登录,判断用户是否登录用户
|
||||
|
||||
@@ -21,6 +21,9 @@ import org.maxkey.authn.AbstractAuthenticationProvider;
|
||||
import org.maxkey.authn.RealmAuthenticationProvider;
|
||||
import org.maxkey.authn.SavedRequestAwareAuthenticationSuccessHandler;
|
||||
import org.maxkey.authn.jwt.AuthJwtService;
|
||||
import org.maxkey.authn.jwt.CongressService;
|
||||
import org.maxkey.authn.jwt.InMemoryCongressService;
|
||||
import org.maxkey.authn.jwt.RedisCongressService;
|
||||
import org.maxkey.authn.online.OnlineTicketService;
|
||||
import org.maxkey.authn.online.OnlineTicketServiceFactory;
|
||||
import org.maxkey.authn.realm.AbstractAuthenticationRealm;
|
||||
@@ -82,8 +85,19 @@ public class AuthenticationAutoConfiguration implements InitializingBean {
|
||||
}
|
||||
|
||||
@Bean(name = "authJwtService")
|
||||
public AuthJwtService authJwtService(AuthJwkConfig authJwkConfig) throws JOSEException {
|
||||
AuthJwtService authJwtService = new AuthJwtService(authJwkConfig);
|
||||
public AuthJwtService authJwtService(
|
||||
AuthJwkConfig authJwkConfig,
|
||||
RedisConnectionFactory redisConnFactory,
|
||||
@Value("${maxkey.server.persistence}") int persistence) throws JOSEException {
|
||||
CongressService congressService;
|
||||
if (persistence == ConstsPersistence.REDIS) {
|
||||
congressService = new RedisCongressService(redisConnFactory);
|
||||
}else {
|
||||
congressService = new InMemoryCongressService();
|
||||
}
|
||||
|
||||
AuthJwtService authJwtService = new AuthJwtService(authJwkConfig,congressService);
|
||||
|
||||
return authJwtService;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user