package com.example.myshiro.service;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.AuthenticationException;

@Service
public class UsersService {
    @Autowired
    private DefaultWebSecurityManager securityManager;

    public void usersLogin(String username,String password) {
            // 1.将SecurityManager对象设置到运行环境中
            SecurityUtils.setSecurityManager(securityManager);
            // 2.获取Subject对象
            Subject subject = SecurityUtils.getSubject();
            // 3.将前端传来的用户名密码封装为Shiro提供的身份对象
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            // 4.shiro认证
            subject.login(token);
    }
}
