前后端分离项目demo
前后端分离demo,整合日常开发所需用到的业务点的实现:github地址
-
v1.0版本(2020-06-13 - 2020-06-14)
1.更新说明:
1.1: Springboot整合shiro,实现用户的用户认证和授权
1.2: 场景描述:用户需要登陆才能访问相关接口,未登录会提示登陆()2.接口介绍:
接口名称 请求地址 作用 登录请求 http://localhost:8080/test/login 传入账号密码登录 登出 http://llocalhost:8080/test/logout 用户退出 加载权限列表 http://llocalhost:8080/test/loadPermission 获取权限列表 提示登陆 http://llocalhost:8080/test/loginRemind 提示登陆,用户未登录会请求到这里 2.1:
加载接口请求图如下:
3.参考:
书籍:<<跟我学shiro>>,张开涛著
springboot整合shiro(完整版)
springboot与shiro整合(示例)4.总结:
4.1:主要是这个类,认证和授权的逻辑都在里面写;权限的校验通过拦截器拦截所访问的接口,获取到目前访问接口的所需权限,调用shiro的Subject相关接口可以校验权限,没有权限会抛相应的异常,捕捉处理即可,另外通过标签注明,标签的左右还有在加载的时候获取所有的权限标签,然后按照角色分配。
package com.ample16.backend.shiro; import com.ample16.backend.entity.User; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; public class MyShiroRealm extends AuthorizingRealm { Boolean cachingEnabled = true; /** * 授权操作 对传入的用户进行授权 * * @param principalCollection * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo(); User primaryPrincipal = (User) principalCollection.getPrimaryPrincipal(); System.out.println("======primaryPrincipal=========" + primaryPrincipal); simpleAuthorInfo.addStringPermission("how_are_you");//给当前用户授权url为hello的权限码 System.out.println("经试验:并不是每次调用接口就会执行,而是调用需要操作subject的permission相关接口就会执行"); return simpleAuthorInfo; } /** * 认证操作 传入token UsernamePasswordToken 包含账号密码 * * @param authcToken * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authcToken) throws AuthenticationException { //获取基于用户名和密码的令牌 //实际上这个authcToken是从LoginController里面currentUser.login(token)传过来的 UsernamePasswordToken token = (UsernamePasswordToken) authcToken; String account = token.getUsername(); String password = new String(token.getPassword()); User user = new User(); if (account.equals("kaka") && password.equals("123456")) { user.setUsername("kaka"); user.setPassword("123456"); } else { user = null; } if (user == null) { throw new AuthenticationException("用户不存在"); } //进行认证,将正确数据给shiro处理 //密码不用自己比对,AuthenticationInfo认证信息对象,一个接口,new他的实现类对象SimpleAuthenticationInfo /* 第一个参数随便放,可以放user对象,程序可在任意位置获取 放入的对象 * 第二个参数必须放密码, * 第三个参数放 当前realm的名字,因为可能有多个realm*/ AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user, user.getPassword(), this.getName()); //AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(user,user.getPassword(),new MySimpleByteSource(account), this.getName()); //清之前的授权信息 super.clearCachedAuthorizationInfo(authcInfo.getPrincipals()); SecurityUtils.getSubject().getSession().setAttribute("userInfo", user); return authcInfo;//返回给安全管理器,securityManager,由securityManager比对数据库查询出的密码和页面提交的密码 //如果有问题,向上抛异常,一直抛到控制器 } }
-
v1.1版本(待更新)