package com.freestyle.easyshiro.method.sample;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.freestyle.common.ResponseEntity;
import com.freestyle.easyshiro.SessionUtils;
import com.freestyle.easyshiro.intercept.method.config.EasyShiroMethodProperties;
import com.freestyle.easyshiro.realms.UserDtoWithPermission;
import com.freestyle.jwt.JwtUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Created by rocklee on 2022/3/10 15:39
*/
@RestController
@RequestMapping("/user")
public class LoginController {
@Resource
private UserService userService;
@Resource
private SessionUtils sessionUtils;
@Resource(name="shiroMethodproperties")
private EasyShiroMethodProperties properties;
@GetMapping("login")
public Object login(@RequestParam(value = "user",required = false)String user, @RequestParam(value = "password",required = false) String password, HttpServletResponse response){
if (user==null){
return "显示登录界面";
}
ThreadContext.put(ThreadContext.SECURITY_MANAGER_KEY,sessionUtils.getSecurityManager());
Subject subject = SecurityUtils.getSubject(); // 获取当前主体
UsernamePasswordToken token = new UsernamePasswordToken(user, password);
try {
subject.login(token); // 登录
if (!StringUtils.isBlank(properties.getJwtTokenName())){
String jwtToken= JwtUtils.getToken(subject.getSession().getId(),properties.getSessionTimeOut(), TimeUnit.SECONDS);
//返回两组token,之后的访问随便用哪个
response.setHeader(properties.getJwtTokenName(), jwtToken);
}
if (!StringUtils.isBlank(properties.getSessionTokenName())){
response.setHeader(properties.getSessionTokenName(),subject.getSession().getId().toString());
}
return ResponseEntity.fromResult(0,"OK");
}
catch (UnknownAccountException |IncorrectCredentialsException e){
return ResponseEntity.fromErr(0,-1,"用户或密码无效");
}
}
@GetMapping("subject")
@RequiresPermissions(logical = Logical.AND, value = {"p1"})
public Object getSubject(HttpServletRequest request) throws JsonProcessingException {
Subject subject=sessionUtils.createSubject(request.getHeader("token"));
UserDtoWithPermission userDtoWithPermission=(UserDtoWithPermission)subject.getPrincipal();
Map<String,Object> values=new HashMap<>();
values.put("account",userDtoWithPermission.getAccount() );
values.put("roles",userDtoWithPermission.getRoles());
values.put("permissions",userDtoWithPermission.getPermissions());
return ResponseEntity.fromResult(0,values);
}
@GetMapping("p1")
public Object p1(){
//ThreadContext.put(THREAD_CONTEXT_SESSION_KEY,"7de6c42d-2b1d-4389-b36d-91fa0bc0281f");
return userService.p1();
}
@GetMapping("p2")
public Object p2(){
return userService.p2();
}
}
评论0