package com.izouma.awesomeadmin.shiro;

import com.izouma.awesomeadmin.model.UserInfo;
import org.activiti.engine.IdentityService;
import org.apache.shiro.authc.*;
import org.springframework.beans.factory.annotation.Autowired;

public class UserPasswordRealm extends BaseRealm {
    @Autowired
    private IdentityService identityService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UserInfo userInfo = null;
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        userInfo = userInfoService.login(userToken.getUsername(), new String(userToken.getPassword()));
        if (userInfo == null) {
            throw new AuthenticationException("用户名或密码错误");
        }
        identityService.setAuthenticatedUserId(userInfo.getId().toString());
        return new SimpleAuthenticationInfo(userInfo, token.getCredentials(), this.getName());
    }
}
