whycxzp
2021-01-08 d04fed7dfceee61b5dd6ba10f23ff16c80458f47
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
package com.whyc.service;
 
import com.whyc.dto.Response;
import com.whyc.util.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
 
@Service
public class LoginService {
 
    public Response login(String userName, String password) {
        UsernamePasswordToken userToken = new UsernamePasswordToken(userName, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(userToken);
        }catch (Exception e){
            return new Response<>().set(1,false);
        }
        if (subject.isAuthenticated()){
            return new Response<>().set(1,true);
        }
        return new Response<>().set(1,false);
    }
 
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }
}