保誠-保戶業務員媒合平台
wayne
2021-11-25 7dbb876d93cef50d0475f332dbf6cc279893b8e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.pollex.pam.service;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
 
import com.pollex.pam.security.jwt.TokenProvider;
import com.pollex.pam.security.token.OtpAuthenticationToken;
import com.pollex.pam.web.rest.vm.OtpAccount;
 
@Service
public class CustomerAuthService {
    
    @Autowired
    AuthenticationManagerBuilder authenticationManagerBuilder;
 
    @Autowired
    TokenProvider tokenProvider;
    
    public String authorize(String account, String indexKey, String otpCode) {
        OtpAccount otpAccount = new OtpAccount(account, indexKey);
        OtpAuthenticationToken authenticationToken = new OtpAuthenticationToken(
            otpAccount,
            otpCode
        );
 
        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String jwt = tokenProvider.createToken(authentication, false);
        return jwt;
    }
}