trustAuthentication

This commit is contained in:
Crystal.Sea
2020-09-10 08:00:45 +08:00
parent b3083adf7d
commit d14a0962f0
12 changed files with 78 additions and 47 deletions

View File

@@ -30,6 +30,7 @@ import java.util.Date;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.DateTime;
import org.maxkey.authn.RealmAuthenticationProvider;
import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.configuration.oidc.OIDCProviderMetadataDetails;
import org.maxkey.constants.ConstantsLoginType;
@@ -47,6 +48,8 @@ public class JwtLoginService {
OIDCProviderMetadataDetails jwtProviderMetadata;
DefaultJwtSigningAndValidationService jwtSignerValidationService;
RealmAuthenticationProvider authenticationProvider ;
public boolean login(String jwt, HttpServletResponse response) {
_logger.debug("jwt : " + jwt);
@@ -93,9 +96,8 @@ public class JwtLoginService {
DateTime now = new DateTime();
if (loginResult && now.isBefore(jwtClaimsSet.getExpirationTime().getTime())) {
if (WebContext.setAuthentication(username, ConstantsLoginType.JWT, "", "", "success")) {
return true;
}
authenticationProvider.trustAuthentication(username, ConstantsLoginType.JWT, "", "", "success");
return true;
}
} catch (java.text.ParseException e) {
// Invalid signed JWT encoding
@@ -198,4 +200,8 @@ public class JwtLoginService {
this.jwtSignerValidationService = jwtSignerValidationService;
}
public void setAuthenticationProvider(RealmAuthenticationProvider authenticationProvider) {
this.authenticationProvider = authenticationProvider;
}
}

View File

@@ -23,6 +23,7 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.DateTime;
import org.maxkey.authn.RealmAuthenticationProvider;
import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.constants.ConstantsLoginType;
import org.maxkey.constants.ConstantsTimeInterval;
@@ -46,6 +47,10 @@ public abstract class AbstractRemeberMeService {
@Autowired
@Qualifier("applicationConfig")
protected ApplicationConfig applicationConfig;
@Autowired
@Qualifier("authenticationProvider")
RealmAuthenticationProvider authenticationProvider ;
// follow function is for persist
public abstract void save(RemeberMe remeberMe);
@@ -112,15 +117,14 @@ public abstract class AbstractRemeberMeService {
DateTime expiryDate = loginDate.plusSeconds(getRemeberMeValidity());
DateTime now = new DateTime();
if (now.isBefore(expiryDate)) {
if (WebContext.setAuthentication(
authenticationProvider.trustAuthentication(
storeRemeberMe.getUsername(),
ConstantsLoginType.REMEBER_ME,
"",
"",
"success")
) {
return updateRemeberMe(remeberMeCookie, response);
}
"success");
return updateRemeberMe(remeberMeCookie, response);
}
return false;
}

View File

@@ -22,6 +22,8 @@ import com.nimbusds.jose.JWEAlgorithm;
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import org.maxkey.authn.RealmAuthenticationProvider;
import org.maxkey.authn.support.jwt.JwtLoginService;
import org.maxkey.configuration.oidc.OIDCProviderMetadataDetails;
import org.maxkey.constants.ConstantsProperties;
@@ -124,11 +126,13 @@ public class JwtAuthnAutoConfiguration implements InitializingBean {
@Bean(name = "jwtLoginService")
public JwtLoginService jwtLoginService(
DefaultJwtSigningAndValidationService jwtSignerValidationService,
OIDCProviderMetadataDetails oidcProviderMetadata) {
JwtLoginService jwkSetKeyStore = new JwtLoginService();
jwkSetKeyStore.setJwtSignerValidationService(jwtSignerValidationService);
jwkSetKeyStore.setJwtProviderMetadata(oidcProviderMetadata);
return jwkSetKeyStore;
OIDCProviderMetadataDetails oidcProviderMetadata,
RealmAuthenticationProvider authenticationProvider) {
JwtLoginService jwtLoginService = new JwtLoginService();
jwtLoginService.setJwtSignerValidationService(jwtSignerValidationService);
jwtLoginService.setJwtProviderMetadata(oidcProviderMetadata);
jwtLoginService.setAuthenticationProvider(authenticationProvider);
return jwtLoginService;
}

View File

@@ -28,7 +28,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.LogFactory;
import org.maxkey.authn.RealmAuthenticationProvider;
import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.domain.UserInfo;
import org.maxkey.util.DateUtils;
@@ -113,7 +112,7 @@ public final class WebContext {
* @param code String
* @param message String
* @return boolean
*/
public static boolean setAuthentication(String username,
String type,
String provider,
@@ -125,7 +124,7 @@ public final class WebContext {
authenticationProvider.trustAuthentication(username, type, provider, code, message);
return isAuthenticated();
}
}*/
public static void setAuthentication(Authentication authentication) {
setAttribute(WebConstants.AUTHENTICATION, authentication);