当前位置: 首页 > news >正文

定制网站和模板网站的区别有哪些好的印花图案设计网站

定制网站和模板网站的区别,有哪些好的印花图案设计网站,快速网页制作,弹幕网站用什么做目录 一、Shiro 简介1.1 Shiro 定义1.2 Shiro 核心组件1.3 Shiro 认证过程 二、SpringBoot集成2.1 集成思路2.2 Maven依赖2.3 自定义 Realm2.4 Shiro 配置类2.5 静态资源映射2.6 AuthController2.7 User 实体2.8 用户接口类2.9 用户接口实现类2.10 OrderController#xff08;… 目录 一、Shiro 简介1.1 Shiro 定义1.2 Shiro 核心组件1.3 Shiro 认证过程 二、SpringBoot集成2.1 集成思路2.2 Maven依赖2.3 自定义 Realm2.4 Shiro 配置类2.5 静态资源映射2.6 AuthController2.7 User 实体2.8 用户接口类2.9 用户接口实现类2.10 OrderController鉴权测试 三、测试测试1跳转登录页面测试2注册用户测试3登录主页测试4权限校验 四、注意 官网地址 https://shiro.apache.org/中文文档 https://www.docs4dev.com/docs/zh/apache-shiro/1.5.3/reference/introduction.htmlSpring集成Shiro官方文档 https://shiro.apache.org/spring-framework.htmlGitHub https://github.com/apache/shiro 一、Shiro 简介 1.1 Shiro 定义 Apache Shiro是一款 Java 安全框架不依赖任何容器可以运行在 Java 项目中它的主要作用是做身份认证、授权、会话管理和加密等操作。 其实不用 Shiro我们使用原生 Java API 就可以实现安全管理使用过滤器去拦截用户的各种请求然后判断是否登录、是否拥有权限即可。但是对于一个大型的系统分散去管理编写这些过滤器的逻辑会比较麻烦不成体系所以需要使用结构化、工程化、系统化的解决方案。 与 Spring Security 相比shiro 属于轻量级框架相对于 Spring Security 简单的多也没有那么复杂。 1.2 Shiro 核心组件 Shiro 的运行机制如下图所示 1UsernamePasswordToken封装用户登录信息根据用户的登录信息创建令牌 token用于验证令牌是否具有合法身份以及相关权限。 2SecurityManager核心部分负责安全认证与授权。 3Subject一个抽象概念包含了用户信息。 4Realm开发者自定义的模块根据项目的需求验证和授权的逻辑在 Realm 中实现。 5AuthenticationInfo用户的角色信息集合认证时使用。 6AuthorizationInfo角色的权限信息集合授权时使用。 7DefaultWebSecurityManager安全管理器开发者自定义的 Realm 需要注入到 DefaultWebSecurityManager 中进行管理才能生效。 8ShiroFilterFactoryBean过滤器工厂Shiro 的基本运行机制是开发者定制规则Shiro 去执行具体的执行操作就是由 ShiroFilterFactoryBean 创建一个个 Filter 对象来完成。 1.3 Shiro 认证过程 二、SpringBoot集成 2.1 集成思路 SpringBoot 集成 Shiro 思路图如下 项目包结构如下 2.2 Maven依赖 !--Shiro-- dependencygroupIdorg.apache.shiro/groupIdartifactIdshiro-spring-boot-starter/artifactIdversion1.12.0/version /dependency2.3 自定义 Realm 自定义 Realm 主要实现了两大模块 认证根据用户名查询密码然后封装返回 SimpleAuthenticationInfo 认证信息。授权根据用户名查询角色和权限然后封装返回 SimpleAuthorizationInfo 授权信息。 CustomRealm.java import com.demo.module.entity.User; import com.demo.module.service.UserService; import com.demo.util.SpringUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.util.ByteSource;/*** p Title CustomRealm* p Description 自定义Realm** author ACGkaka* date 2023/10/15 12:42*/ public class CustomRealm extends AuthorizingRealm {/*** 认证*/Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {// 获取用户名String principal (String) authenticationToken.getPrincipal();// 根据用户名查询数据库UserService userService SpringUtils.getBean(UserService.class);User user userService.findByUsername(principal);if (user ! null) {return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),ByteSource.Util.bytes(user.getSalt()), this.getName());}return null;}/*** 授权*/Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {// 获取用户名String principal (String) principalCollection.getPrimaryPrincipal();if (admin.equals(principal)) {// 管理员拥有所有权限SimpleAuthorizationInfo info new SimpleAuthorizationInfo();info.addRole(admin);info.addStringPermission(admin:*);info.addRole(user);info.addStringPermission(user:find:*);return info;}return null;} }2.4 Shiro 配置类 配置类中指定了如下内容 需要进行鉴权的资源路径指定自定义 Realm加密规则。 ShiroConfig.java import com.demo.config.shiro.realm.CustomRealm; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.realm.Realm; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import java.util.HashMap; import java.util.Map;/*** p Title ShiroConfig* p Description Shiro配置类** author ACGkaka* date 2023/10/15 12:44*/ Configuration public class ShiroConfig {/*** ShiroFilter过滤所有请求*/Beanpublic ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {ShiroFilterFactoryBean shiroFilterFactoryBean new ShiroFilterFactoryBean();// 给ShiroFilter配置安全管理器shiroFilterFactoryBean.setSecurityManager(securityManager);// 配置系统公共资源、系统受限资源公共资源必须在受限资源上面不然会造成死循环MapString, String map new HashMap();// 系统公共资源map.put(/login, anon);map.put(/register, anon);map.put(/static/**, anon);// 受限资源map.put(/**, authc);// 设置认证界面路径shiroFilterFactoryBean.setLoginUrl(/login);shiroFilterFactoryBean.setFilterChainDefinitionMap(map);return shiroFilterFactoryBean;}/*** 创建安全管理器*/Beanpublic DefaultWebSecurityManager securityManager(Realm realm) {DefaultWebSecurityManager securityManager new DefaultWebSecurityManager();securityManager.setRealm(realm);return securityManager;}/*** 创建自定义Realm*/Beanpublic Realm realm() {CustomRealm realm new CustomRealm();// 设置使用哈希凭证匹配HashedCredentialsMatcher credentialsMatcher new HashedCredentialsMatcher();// 设置使用MD5加密算法credentialsMatcher.setHashAlgorithmName(MD5);// 设置散列次数加密次数credentialsMatcher.setHashIterations(1024);realm.setCredentialsMatcher(credentialsMatcher);return realm;} }2.5 静态资源映射 静态资源映射主要将 css、js 等静态文件夹映射到浏览器端方便页面加载。 WebConfiguration.java import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.util.ResourceUtils; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** SpringBoot静态路径配置** author ACGkaka* date 2019/11/27 15:38*/ Configuration Primary public class WebConfiguration implements WebMvcConfigurer {/*** 访问外部文件配置*/Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler(/static/css/**).addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX /static/css/);registry.addResourceHandler(/static/js/**).addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX /static/js/);WebMvcConfigurer.super.addResourceHandlers(registry);} }2.6 AuthController 主要用于进行 thymeleaf 模板引擎的页面跳转以及登录、注册、退出登录等功能的实现。 AuthController.java import com.demo.module.entity.User; import com.demo.module.service.UserService; import lombok.AllArgsConstructor; 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.subject.Subject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping;import javax.annotation.Resource;/*** p Title IndexController* p Description 鉴权Controller** author ACGkaka* date 2019/10/23 20:23*/ Controller AllArgsConstructor public class AuthController {Resourceprivate UserService userService;/*** 默认跳转主页*/GetMapping(/)public String showIndex() {return redirect:/index;}/*** 主页*/GetMapping(/index)public String index() {return index.html;}/*** 主页*/GetMapping(/login)public String login() {return login.html;}/*** 注册页*/GetMapping(/register)public String register() {return /register.html;}/*** 登录*/PostMapping(/login)public String login(String username, String password) {// 获取主题对象Subject subject SecurityUtils.getSubject();try {subject.login(new UsernamePasswordToken(username, password));System.out.println(登录成功);return redirect:/index;} catch (UnknownAccountException e) {System.out.println(用户错误);} catch (IncorrectCredentialsException e) {System.out.println(密码错误);}return redirect:/login;}/*** 注册*/PostMapping(/register)public String register(User user) {userService.register(user);return redirect:/login.html;}/*** 退出登录*/GetMapping(/logout)public String logout() {Subject subject SecurityUtils.getSubject();subject.logout();return redirect:/login.html;} }2.7 User 实体 封装了用户的基本属性。 User.java import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data;import java.io.Serializable; import java.time.LocalDateTime;/*** p* 用户表* /p** author ACGkaka* since 2021-04-25*/ Data TableName(t_user) public class User implements Serializable {private static final long serialVersionUID 1L;/*** 主键*/TableId(value id, type IdType.AUTO)private Long id;/*** 用户名*/private String username;/*** 密码*/private String password;/*** 随机盐*/private String salt;/*** 创建时间*/private LocalDateTime createTime;/*** 更新时间*/private LocalDateTime updateTime; }2.8 用户接口类 封装了注册用户新增和根据用户名查找接口。 UserService.java import com.baomidou.mybatisplus.extension.service.IService; import com.demo.module.entity.User;/*** 用户表 服务类*/ public interface UserService extends IServiceUser {/*** 注册* param user 用户*/void register(User user);/*** 根据用户名查询用户* param principal 用户名* return 用户*/User findByUsername(String principal); }2.9 用户接口实现类 实现了注册用户新增和根据用户名查找功能。 UserServiceImpl.java import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.demo.module.entity.User; import com.demo.module.mapper.UserMapper; import com.demo.module.service.UserService; import com.demo.util.SaltUtils; import org.apache.shiro.crypto.hash.Md5Hash; import org.springframework.stereotype.Service;/*** p* 用户表 服务实现类* /p** author ACGkaka* since 2021-04-25*/ Service public class UserServiceImpl extends ServiceImplUserMapper, User implements UserService {Overridepublic void register(User user) {// 注册用户checkRegisterUser(user);// 1.生成随机盐String salt SaltUtils.getSalt(8);// 2.将随机盐保存到数据库user.setSalt(salt);// 3.明文密码进行MD5 salt hash散列次数Md5Hash md5Hash new Md5Hash(user.getPassword(), salt, 1024);user.setPassword(md5Hash.toHex());// 4.保存用户this.save(user);}Overridepublic User findByUsername(String principal) {// 根据用户名查询用户LambdaQueryWrapperUser queryWrapper new LambdaQueryWrapper();queryWrapper.eq(User::getUsername, principal);return this.getOne(queryWrapper);}// ------------------------------------------------------------------------------------------// 内部方法// ------------------------------------------------------------------------------------------/*** 校验注册用户* param user 用户*/private void checkRegisterUser(User user) {if (user null) {throw new RuntimeException(用户信息不能为空);}if (user.getUsername() null || .equals(user.getUsername())) {throw new RuntimeException(用户名不能为空);}if (user.getPassword() null || .equals(user.getPassword())) {throw new RuntimeException(密码不能为空);}// 判断用户名是否已存在User existUser this.getOne(new UpdateWrapperUser().eq(username, user.getUsername()));if (existUser ! null) {throw new RuntimeException(用户名已存在);}} }2.10 OrderController鉴权测试 在自定义 Realm 配置好用户权限后用于测试对用户权限和角色权限的控制。 OrderController.java import com.demo.common.Result; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresRoles; import org.apache.shiro.subject.Subject; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** p Title OrderController* p Description 订单控制器** author ACGkaka* date 2023/10/15 17:15*/ RestController RequestMapping(/order) public class OrderController {/*** 新增订单代码实现权限判断*/GetMapping(/add)public ResultObject add() {Subject subject SecurityUtils.getSubject();if (subject.hasRole(admin)) {return Result.succeed().setData(新增订单成功);} else {return Result.failed().setData(操作失败无权访问);}}/*** 编辑订单注解实现权限判断*/RequiresRoles({admin, user}) // 用来判断角色同时拥有admin和user角色才能访问RequiresPermissions(user:edit:01) // 用来判断权限拥有user:edit:01权限才能访问GetMapping(/edit)public String edit() {System.out.println(编辑订单);return redirect:/index;} }三、测试 测试1跳转登录页面 访问地址http://localhost:8081我们配置了根路径默认访问主页由于用户没有登录会默认跳转登录页面。 测试2注册用户 访问地址http://localhost:8081/register输入用户名和密码后系统会在数据库中新增用户信息自动跳转登录页面输入刚才的用户名和密码即可登录。 测试3登录主页 输入正确的用户名密码即可登录至主页。 测试4权限校验 我们在主页中加入了 OrderController 中的接口用于测试权限校验。 当使用非 admin 用户进行登录的时候代码实现权限判断的 /add 接口报错如下 Shiro 注解实现权限判断的 /edit 接口报错如下 从提示信息可以看到返回了状态码 500报错信息为当前用户的主体没有 admin 权限。 由此可见两种鉴权结果均成功生效具体使用哪一种由业务场景来定。 四、注意 由于 Shiro 核心是通过 Session 来实现用户登录的所以有很多场景不支持比如 1Session 默认存储在内存中重启应用后所有用户登录状态会失效 2默认配置不支持分布式需要分布式部署的时候可以通过 Redis 或数据库方式来同步 Session 会话。 整理完毕完结撒花~ 参考地址 1.SpringBoot之整合Shiro(最详细)https://blog.csdn.net/Yearingforthefuture/article/details/117384035 2.超详细 Spring Boot 整合 Shiro 教程https://cloud.tencent.com/developer/article/1643122 3.springboot整合shiro完整版https://blog.csdn.net/w399038956/article/details/120434244 4.Shiro安全框架【快速入门】就这一篇https://zhuanlan.zhihu.com/p/54176956
http://www.zqtcl.cn/news/558706/

相关文章:

  • 桂林城乡建设局网站在线咨询免费
  • 长治网站设计制作网站ps怎么做网站导航内嵌式
  • 网站 橙色前台网站开发
  • 滨海网站建设服务商电子商务网站建设与维护pdf
  • 企业网站建设方案效果h5网页制作app
  • 国内搜索引擎网站免费无线
  • 龙岩做网站价格室内建筑设计
  • 闲鱼上面给人做网站造退款微信登录建设银行网站
  • 无锡网站推广公司网络营销课程设置
  • dede 网站根目录北京好的设计公司
  • 网站关键词重复wordpress 影响力
  • 外包商网站怎么做php网站转移
  • 怎么做自己的网站推广产品企业建站 平台
  • 河北做网站公司网站建设团队扬州
  • 114物流网站怎么做免费注册163免费邮箱申请
  • 做网站要以单位手机发博客wordpress
  • 莆田网站建设莆田seo管理系统培训
  • 有一个网站自己做链接获取朋友位置网站关键词数量减少
  • 毕设网站建设论文小程序开发模板
  • 广州网页模板建站电商平台谈双11变冷
  • 用.cc做网站官网可以吗2003系统网站建设
  • 创意网站推荐新手网站
  • 网站编程好学吗免费下载app并安装
  • 广州专业网站制作设计网站建设分几种
  • 有没有专业做艺术品的网站长沙人才市场招聘信息
  • 河池做网站通过邮箱查注册网站
  • 金融互助网站开发网上免费设计效果图
  • 网站开发 例子施工企业质量管理体系应按照我国
  • 义乌建设网站网络营销推广有哪些方法
  • 宿迁建设局网站a类证查询怎么自己搭建梯子