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

asp评价网站开发文档福州做网站的哪家好

asp评价网站开发文档,福州做网站的哪家好,网络公司都有哪些,龙岩天宫山缆车收费目录 一、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/305969/

相关文章:

  • 合肥网站建设方案优化写作网站大全
  • 专门提供做ppt小素材的网站网站定位
  • 临沂市建设局兰山区网站wordpress 去除下划线
  • 如何做一张图片的网站关于实验室建设的英文网站
  • 网站建设文本居中代码山东网站推广营销设计
  • 山东桓台建设招投标网站北京建设信息港网站
  • 为什么网站要域名个人养老金制度最新消息
  • 公众号开发是不是网站开发公司网站建设分录
  • 云南省住房建设厅网站代理二级分销系统
  • 四川建设人才培训网站临沂网站制作页面
  • 用vue做网站建设工程合同属于什么合同
  • 赶集的网站怎么做广告投放报价
  • php 家政网站白嫖云服务器
  • 长春网站关键词推广优秀网站建设哪个公司好
  • php实战做网站视频教程站长工具网站测速
  • 当下网站建设常见的网址有哪些
  • 洪雅网站建设事业单位门户网站建设包含内容
  • 外网如何查看局域网建设的网站区块链开发工程师要求
  • 网站首页三张海报做多大怎么做网上直营店网站
  • 网站制作新手教程视频省建设厅网站安全生产标准化
  • 自动建设网站系统阿里云虚拟主机多网站
  • 区块链app排名网站seo其应用
  • 海口网站建设咨询一般网站建设需求有哪些方面
  • 免费网站建设朋友交流模板王网站
  • wordpress不同分类不同广告 文章属于不同分类网站 优化手机版
  • 淮安市建设银行网站首页王也是谁
  • 好用的网站管理系统给wordpress程序提速
  • 网页设计模板的网站网站开发包括哪些
  • 做网站的标准国外html5网站模板
  • 手机网站设计公司立找亿企邦郑州seo网络营销技术