shardingsphere-elasticjob-cloud-ui/shardingsphere-elasticjob-cloud-ui-backend/src/main/java/org/apache/shardingsphere/elasticjob/cloud/ui/security/UserAuthenticationService.java [38:95]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Component
@ConfigurationProperties(prefix = "auth")
@Setter
public final class UserAuthenticationService {
    
    private static final String JWT_TOKEN_ISSUER = "shardingsphere-elasticjob-ui";
    
    private final Algorithm algorithm = Algorithm.HMAC256(RandomStringUtils.randomAlphanumeric(256));
    
    private final JWTVerifier verifier = JWT.require(algorithm).withIssuer(JWT_TOKEN_ISSUER).build();
    
    private String username;
    
    private String password;
    
    private int tokenExpiresAfterSeconds = 3600;
    
    /**
     * Check user.
     *
     * @param userAccount user account
     * @return check success or failure
     */
    public AuthenticationResult checkUser(final UserAccount userAccount) {
        if (null == userAccount || Strings.isNullOrEmpty(userAccount.getUsername()) || Strings.isNullOrEmpty(userAccount.getPassword())) {
            return new AuthenticationResult(null, null, false);
        }
        if (username.equals(userAccount.getUsername()) && password.equals(userAccount.getPassword())) {
            return new AuthenticationResult(username, password, true);
        }
        return new AuthenticationResult(null, null, false);
    }
    
    /**
     * Get user authentication token.
     *
     * @return authentication token
     */
    public String getToken(final String username) {
        Map<String, Object> payload = new HashMap<>(1, 1);
        payload.put("username", username);
        Date expiresAt = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(tokenExpiresAfterSeconds));
        return JWT.create().withExpiresAt(expiresAt).withIssuer(JWT_TOKEN_ISSUER).withPayload(payload).sign(algorithm);
    }
    
    /**
     * Check if token is valid.
     *
     * @param token token
     * @return is valid
     */
    public boolean isValidToken(final String token) {
        try {
            verifier.verify(token);
        } catch (JWTVerificationException ignored) {
            return false;
        }
        return true;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



shardingsphere-elasticjob-lite-ui/shardingsphere-elasticjob-lite-ui-backend/src/main/java/org/apache/shardingsphere/elasticjob/lite/ui/security/UserAuthenticationService.java [38:95]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Component
@ConfigurationProperties(prefix = "auth")
@Setter
public final class UserAuthenticationService {
    
    private static final String JWT_TOKEN_ISSUER = "shardingsphere-elasticjob-ui";
    
    private final Algorithm algorithm = Algorithm.HMAC256(RandomStringUtils.randomAlphanumeric(256));
    
    private final JWTVerifier verifier = JWT.require(algorithm).withIssuer(JWT_TOKEN_ISSUER).build();
    
    private String username;
    
    private String password;
    
    private int tokenExpiresAfterSeconds = 3600;
    
    /**
     * Check user.
     *
     * @param userAccount user account
     * @return check success or failure
     */
    public AuthenticationResult checkUser(final UserAccount userAccount) {
        if (null == userAccount || Strings.isNullOrEmpty(userAccount.getUsername()) || Strings.isNullOrEmpty(userAccount.getPassword())) {
            return new AuthenticationResult(null, null, false);
        }
        if (username.equals(userAccount.getUsername()) && password.equals(userAccount.getPassword())) {
            return new AuthenticationResult(username, password, true);
        }
        return new AuthenticationResult(null, null, false);
    }
    
    /**
     * Get user authentication token.
     *
     * @return authentication token
     */
    public String getToken(final String username) {
        Map<String, Object> payload = new HashMap<>(1, 1);
        payload.put("username", username);
        Date expiresAt = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(tokenExpiresAfterSeconds));
        return JWT.create().withExpiresAt(expiresAt).withIssuer(JWT_TOKEN_ISSUER).withPayload(payload).sign(algorithm);
    }
    
    /**
     * Check if token is valid.
     *
     * @param token token
     * @return is valid
     */
    public boolean isValidToken(final String token) {
        try {
            verifier.verify(token);
        } catch (JWTVerificationException ignored) {
            return false;
        }
        return true;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



