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();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
easy-shiro的登录认证、鉴权例子,
共32个文件
class:10个
java:6个
yml:4个
需积分: 10 0 下载量 130 浏览量
2022-04-09
16:12:30
上传
评论
收藏 45KB ZIP 举报
温馨提示
easy-shiro的登录认证、鉴权例子,或者直接用SessionTokenUtils.setToken传入sessionid进行鉴权。
资源详情
资源评论
资源推荐
收起资源包目录
easy-shiro-intercept-method-sample.zip (32个子文件)
easy-shiro-intercept-method-sample
pom.xml 2KB
target
test-classes
com
freestyle
easyshiro
method
sample
AppTest.class 2KB
maven-status
maven-compiler-plugin
compile
default-compile
inputFiles.lst 786B
createdFiles.lst 0B
testCompile
default-testCompile
inputFiles.lst 143B
createdFiles.lst 0B
generated-test-sources
test-annotations
classes
easy-shiro.yml 539B
config
logback.xml 4KB
application.yml 25B
com
freestyle
easyshiro
method
sample
config
MyAuthenticationInfoServiceImpl$3.class 867B
MyAuthenticationInfoServiceImpl$1.class 867B
LogbackColorful.class 1KB
MyAuthenticationInfoServiceImpl$4.class 861B
MyAuthenticationInfoServiceImpl.class 2KB
MyAuthenticationInfoServiceImpl$2.class 846B
UserService.class 941B
LoginController.class 5KB
TokenTestApplication.class 951B
caffeine.properties 208B
maven-archiver
pom.properties 85B
easy-shiro-dubbo-sample-2.6.4.jar 12KB
generated-sources
annotations
easy-shiro-intercept-method-sample.iml 81B
src
test
java
com
freestyle
easyshiro
method
sample
AppTest.java 1KB
main
resources
easy-shiro.yml 539B
config
logback.xml 4KB
application.yml 25B
caffeine.properties 208B
java
com
freestyle
easyshiro
method
sample
config
MyAuthenticationInfoServiceImpl.java 1KB
LogbackColorful.java 1006B
LoginController.java 4KB
UserService.java 669B
TokenTestApplication.java 577B
共 32 条
- 1
OK_boom
- 粉丝: 6771
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0