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

青岛城市建设集团网站90设计网官网登录

青岛城市建设集团网站,90设计网官网登录,内蒙古城乡建设部网站首页,手机app软件开发价格文章目录 前言一、shiro简介二、环境搭建2.1.数据库2.1.1user用户表2.1.2user_role用户角色关系表2.1.3role角色表2.1.4role_permission角色权限关系表2.1.5permission权限表 2.2导坐标2.3实体类2.3.1User2.3.2Role2.3.3Permission 2.4MVC三层2.4.1User2.4.1.1mapper层2.4.1.2s… 文章目录 前言一、shiro简介二、环境搭建2.1.数据库2.1.1user用户表2.1.2user_role用户角色关系表2.1.3role角色表2.1.4role_permission角色权限关系表2.1.5permission权限表 2.2导坐标2.3实体类2.3.1User2.3.2Role2.3.3Permission 2.4MVC三层2.4.1User2.4.1.1mapper层2.4.1.2service层2.4.1.3controller层 2.4.2Role2.4.2.1mapper层2.4.2.2service层2.4.2.3controller层 2.3.3Permission2.4.3.1mapper层2.4.3.2service层2.4.3.3controller层 2.5.加密工具类2.6.全局异常处理器2.7自定义Realm2.8shiro配置类 三、案例演示3.1 登录认证3.2 权限认证 总结 前言 近期对Springboot框架的学习中为了更好的学习理解Springsecurity中间件先学习了一下“老派”的shiro安全框架本文章将通过注解的方式实现基础的用户认证和角色授权案例 一、shiro简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。 二、环境搭建 2.1.数据库 2.1.1user用户表 2.1.2user_role用户角色关系表 2.1.3role角色表 2.1.4role_permission角色权限关系表 2.1.5permission权限表 2.2导坐标 propertiesjava.version1.8/java.version!--shiro--shiro.version1.3.2/shiro.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-logging/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.29/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.3/version/dependency!-- SECURITY begin --dependencygroupIdorg.apache.shiro/groupIdartifactIdshiro-core/artifactIdversion${shiro.version}/version/dependencydependencygroupIdorg.apache.shiro/groupIdartifactIdshiro-spring/artifactIdversion${shiro.version}/version/dependencydependencygroupIdorg.apache.shiro/groupIdartifactIdshiro-web/artifactIdversion${shiro.version}/version/dependencydependencygroupIdorg.apache.shiro/groupIdartifactIdshiro-ehcache/artifactIdversion${shiro.version}/version/dependency!-- SECURITY end --/dependencies2.3实体类 2.3.1User TableName(pe_user) Data AllArgsConstructor NoArgsConstructor public class User {TableId(value id,type IdType.AUTO)private int id;private String username;private String password;private String salt;TableField(exist false)private SetRole roles; }2.3.2Role TableName(pe_role) Data AllArgsConstructor NoArgsConstructor public class Role {TableId(value id,type IdType.AUTO)private int id;private String name;private String code;private String description;TableField(exist false)private SetPermission permissions; } 2.3.3Permission TableName(pe_permission) Data AllArgsConstructor NoArgsConstructor public class Permission {TableId(value id,type IdType.AUTO)private int id;private String name;private String code;private String description; }2.4MVC三层 2.4.1User 2.4.1.1mapper层 public interface UserMapper extends BaseMapperUser {Select(select * from pe_user where username #{name})User findUserByName(String name);//级联查询Results({Result(column id,property id),Result(column username,property username),Result(column password,property password),Result(column salt,property salt),Result(column id,property roles,manyMany(select com.apesource.shirostudy_demo_05.dao.RoleMapper.findRoleIdsByUserId))})Select(select * from pe_user where id #{id})User findUserDetailById(Param(id) int id); }2.4.1.2service层 public interface IUserService extends IServiceUser {User findUserByName(String name);User findUserDetailById(int id); }Service SuppressWarnings(all) public class UserServiceImp extends ServiceImplUserMapper, User implements IUserService {AutowiredUserMapper userMapper;AutowiredIRoleService roleService;Overridepublic User findUserByName(String name) {User user userMapper.findUserByName(name);return user;}Overridepublic User findUserDetailById(int id) {//根据userid查询userUser user userMapper.findUserDetailById(id);//返回return user;} }2.4.1.3controller层 RestController public class UserController {Autowiredprivate IUserService userService;//个人主页RequiresPermissions(user-home)RequestMapping(value /user/home)public String home() {return 访问个人主页成功;}//根据名字查询用户基本信息RequiresPermissions(user-find)RequestMapping(value /user/userByName/{name})public String findUserByName(PathVariable String name) {System.out.println(name);User user userService.findUserByName(name);return user.toString();}//更新RequiresPermissions(user-update)RequestMapping(value /user/{id}, method RequestMethod.PUT)public String update(PathVariable String id) {return 更新用户成功;}//删除RequiresPermissions(user-delete)RequestMapping(value /user/{id}, method RequestMethod.DELETE)public String delete(PathVariable String id) {return 删除用户成功;}//根据id查询用户详细信息RequiresPermissions(user-find)RequestMapping(value /user/userById/{id})public String findUserDetailById(PathVariable int id) {return userService.findUserDetailById(id).toString();}//登录页面RequestMapping(value /login)public String login(User user) {try {//1.构造登录令牌UsernamePasswordToken token new UsernamePasswordToken(user.getUsername(), user.getPassword());//2.//2.获取subjectSubject subject SecurityUtils.getSubject();//3.调用subject进行登录subject.login(token);return 登录成功;} catch (AuthenticationException e) {return 用户名或密码错误;}}//未登陆与未授权页面RequestMapping(value /autherror)public String autherror() {return 未登录;}}2.4.2Role 2.4.2.1mapper层 public interface RoleMapper extends BaseMapperRole {Results({Result(column id,property id),Result(column name,property name),Result(column code,property code),Result(column description,property description),Result(column id,property permissions,many Many(select com.apesource.shirostudy_demo_05.dao.PermissionMapper.findPermissionIdsByRoleId))})Select(SELECT * FROM pe_role WHERE id IN (SELECT role_id FROM pe_user_role WHERE user_id #{id}))SetRole findRoleIdsByUserId(int id); }2.4.2.2service层 public interface IRoleService extends IServiceRole {SetRole findRoleIdsByUserId(int id); } Service SuppressWarnings(all) public class RoleServiceImp extends ServiceImplRoleMapper, Role implements IRoleService {AutowiredRoleMapper roleMapper;AutowiredIPermissionService permissionService;Overridepublic SetRole findRoleIdsByUserId(int id) {SetRole roles roleMapper.findRoleIdsByUserId(id);return roles;} }2.4.2.3controller层 由于不演示无。 2.3.3Permission 2.4.3.1mapper层 public interface PermissionMapper extends BaseMapperPermission {Results({Result(column id,property id),Result(column name,property name),Result(column code,property code),Result(column description,property description),})Select(SELECT * FROM pe_permission WHERE id IN (SELECT permission_id FROM pe_role_permission WHERE role_id #{id} ) )SetPermission findPermissionIdsByRoleId(int id); }2.4.3.2service层 public interface IPermissionService extends IServicePermission {SetPermission findPermissionIdsByRoleId(int id); } Service public class PermissionServiceImp extends ServiceImplPermissionMapper, Permission implements IPermissionService {Autowired(required false)PermissionMapper permissionMapper;Overridepublic SetPermission findPermissionIdsByRoleId(int id) {SetPermission permissionIds permissionMapper.findPermissionIdsByRoleId(id);return permissionIds;} }2.4.3.3controller层 由于不演示无。 2.5.加密工具类 public class DigestsUtil {public static final String SHA1 SHA-1;public static final Integer COUNTS 369;/*** Description sha1方法* param input 需要散列字符串* param salt 盐字符串* return*/public static String show(String input, String salt) {return new SimpleHash(SHA1, input, salt,COUNTS).toString();}/*** Description 随机获得salt字符串* return*/public static String generateSalt(){SecureRandomNumberGenerator randomNumberGenerator new SecureRandomNumberGenerator();return randomNumberGenerator.nextBytes().toHex();}/*** Description 生成密码字符密文和salt密文* param* return*/public static MapString,String entryptPassword(String passwordPlain) {MapString,String map new HashMap();String salt generateSalt();String password show(passwordPlain,salt);map.put(salt, salt);map.put(password, password);return map;} }2.6.全局异常处理器 ControllerAdvice public class BaseExceptionHandler {ExceptionHandler(value AuthorizationException.class)ResponseBodypublic String error(HttpServletRequest request, HttpServletResponse response, AuthorizationException e){return 未授权;} }2.7自定义Realm public class MyRealm extends AuthorizingRealm {AutowiredIUserService userService;Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {int userId (int) principalCollection.getPrimaryPrincipal();User user userService.findUserDetailById(userId);SetString roles new HashSet();SetString permissions new HashSet();user.getRoles().stream().forEach(role - {roles.add(role.getCode());role.getPermissions().stream().forEach(permission - {permissions.add(permission.getCode());});});SimpleAuthorizationInfo info new SimpleAuthorizationInfo();info.addRoles(roles);info.addStringPermissions(permissions);return info;}/*** 认证方法* 参数传递的用户名密码*/Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {//1.获取登录的用户名密码tokenUsernamePasswordToken token (UsernamePasswordToken) authenticationToken;String username token.getUsername();//2.根据用户名查询数据库//mybatis情景下:user对象中包含IDnamepwd(匿名)User user userService.findUserByName(username);if(user ! null){SimpleAuthenticationInfo info new SimpleAuthenticationInfo(user.getId(),user.getPassword(), ByteSource.Util.bytes(user.getSalt()),myRealm);return info;}return null;}/*** Description 自定义密码比较器* param* return* bean标签 init-method属性*/PostConstructpublic void initCredentialsMatcher() {//指定密码算法HashedCredentialsMatcher hashedCredentialsMatcher new HashedCredentialsMatcher(DigestsUtil.SHA1);//指定迭代次数hashedCredentialsMatcher.setHashIterations(DigestsUtil.COUNTS);//生效密码比较器setCredentialsMatcher(hashedCredentialsMatcher);} }2.8shiro配置类 Configuration public class ShiroConfiguration {/*** 1.创建shiro自带cookie对象*/Beanpublic SimpleCookie sessionIdCookie(){SimpleCookie simpleCookie new SimpleCookie();simpleCookie.setName(ShiroSession);return simpleCookie;}//2.创建realmBeanpublic MyRealm getRealm(){return new MyRealm();}/*** 3.创建会话管理器*/Beanpublic DefaultWebSessionManager sessionManager(){DefaultWebSessionManager sessionManager new DefaultWebSessionManager();sessionManager.setSessionValidationSchedulerEnabled(false);sessionManager.setSessionIdCookieEnabled(true);sessionManager.setSessionIdCookie(sessionIdCookie());sessionManager.setGlobalSessionTimeout(3600000);return sessionManager;}//4.创建安全管理器Beanpublic SecurityManager defaultWebSecurityManager() {DefaultWebSecurityManager securityManager new DefaultWebSecurityManager();securityManager.setRealm(getRealm());securityManager.setSessionManager(sessionManager());return securityManager;}/*** 5.保证实现了Shiro内部lifecycle函数的bean执行*/Bean(name lifecycleBeanPostProcessor)public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {return new LifecycleBeanPostProcessor();}/*** 6.开启对shiro注解的支持* AOP式方法级权限检查*/BeanDependsOn(lifecycleBeanPostProcessor)public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator new DefaultAdvisorAutoProxyCreator();defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);return defaultAdvisorAutoProxyCreator;}/*** 7.配合DefaultAdvisorAutoProxyCreator事项注解权限校验*/Beanpublic AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor new AuthorizationAttributeSourceAdvisor();authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager());return authorizationAttributeSourceAdvisor;}//8.配置shiro的过滤器工厂再web程序中shiro进行权限控制全部是通过一组过滤器集合进行控制Beanpublic ShiroFilterFactoryBean shiroFilter() {//1.创建过滤器工厂ShiroFilterFactoryBean filterFactory new ShiroFilterFactoryBean();//2.设置安全管理器filterFactory.setSecurityManager(defaultWebSecurityManager());//3.通用配置跳转登录页面为授权跳转的页面filterFactory.setLoginUrl(/autherror);//跳转url地址//4.设置过滤器集合//key 拦截的url地址//value 过滤器类型MapString,String filterMap new LinkedHashMap();//key请求规则 value过滤器名称filterMap.put(/login,anon);//当前请求地址可以匿名访问filterMap.put(/user/**,authc);//当前请求地址必须认证之后可以访问//在过滤器工程内设置系统过滤器filterFactory.setFilterChainDefinitionMap(filterMap);return filterFactory;}} 三、案例演示 用户密码均是123456加密后保存至数据库 3.1 登录认证 成功登录 失败登录 3.2 权限认证 由于刚刚登录的信息有管理员和员工的身份所以其所有功能都能使用。 有权限时 无权限时 无权限时会被全局异常处理器拦截并作出响应的响应。 总结 通过对shrio的学习更好的理解了安全认证以及鉴权授权的流程大概了解了安全认证的机制对我之后学习理解Springsecurity有更好的帮助。
文章转载自:
http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn
http://www.morning.daidudu.com.gov.cn.daidudu.com
http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn
http://www.morning.aa1585.com.gov.cn.aa1585.com
http://www.morning.rmltt.cn.gov.cn.rmltt.cn
http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn
http://www.morning.rxnr.cn.gov.cn.rxnr.cn
http://www.morning.csnch.cn.gov.cn.csnch.cn
http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn
http://www.morning.kwqt.cn.gov.cn.kwqt.cn
http://www.morning.tsrg.cn.gov.cn.tsrg.cn
http://www.morning.kndyz.cn.gov.cn.kndyz.cn
http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn
http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn
http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn
http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn
http://www.morning.zrgx.cn.gov.cn.zrgx.cn
http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn
http://www.morning.trjr.cn.gov.cn.trjr.cn
http://www.morning.mhnb.cn.gov.cn.mhnb.cn
http://www.morning.wgrm.cn.gov.cn.wgrm.cn
http://www.morning.yydzk.cn.gov.cn.yydzk.cn
http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn
http://www.morning.fglth.cn.gov.cn.fglth.cn
http://www.morning.wkcl.cn.gov.cn.wkcl.cn
http://www.morning.jwdys.cn.gov.cn.jwdys.cn
http://www.morning.bchfp.cn.gov.cn.bchfp.cn
http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn
http://www.morning.sldrd.cn.gov.cn.sldrd.cn
http://www.morning.rdmz.cn.gov.cn.rdmz.cn
http://www.morning.btlsb.cn.gov.cn.btlsb.cn
http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn
http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn
http://www.morning.plkrl.cn.gov.cn.plkrl.cn
http://www.morning.qcslh.cn.gov.cn.qcslh.cn
http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn
http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn
http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn
http://www.morning.tndhm.cn.gov.cn.tndhm.cn
http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn
http://www.morning.qmncj.cn.gov.cn.qmncj.cn
http://www.morning.lkhfm.cn.gov.cn.lkhfm.cn
http://www.morning.zrqs.cn.gov.cn.zrqs.cn
http://www.morning.ghgck.cn.gov.cn.ghgck.cn
http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn
http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn
http://www.morning.bpmtl.cn.gov.cn.bpmtl.cn
http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn
http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn
http://www.morning.xylxm.cn.gov.cn.xylxm.cn
http://www.morning.krxzl.cn.gov.cn.krxzl.cn
http://www.morning.jglqn.cn.gov.cn.jglqn.cn
http://www.morning.tpdg.cn.gov.cn.tpdg.cn
http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn
http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn
http://www.morning.qrndh.cn.gov.cn.qrndh.cn
http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com
http://www.morning.srmdr.cn.gov.cn.srmdr.cn
http://www.morning.gctgc.cn.gov.cn.gctgc.cn
http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn
http://www.morning.cfnht.cn.gov.cn.cfnht.cn
http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn
http://www.morning.dkqbc.cn.gov.cn.dkqbc.cn
http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn
http://www.morning.ncfky.cn.gov.cn.ncfky.cn
http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn
http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn
http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn
http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn
http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn
http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn
http://www.morning.nstml.cn.gov.cn.nstml.cn
http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn
http://www.morning.jkzq.cn.gov.cn.jkzq.cn
http://www.morning.qjdqj.cn.gov.cn.qjdqj.cn
http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn
http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn
http://www.morning.ffrys.cn.gov.cn.ffrys.cn
http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn
http://www.morning.krgjc.cn.gov.cn.krgjc.cn
http://www.tj-hxxt.cn/news/242407.html

相关文章:

  • 做网站外包最牛的公司wordpress安装博客步骤
  • 做营销型网站要多少钱民宿网站的建设
  • 锦州网站建设案例云南瑞丽最新政策
  • 重庆网站平台建设wordpress 珠宝
  • 怎么做时光网站会展网站建设的步骤
  • 怎么用FTP做网站关于省钱的网站名字
  • 适合设计制作公司的网站asp远吗常用网站大全
  • 网站开发背景论文青少年编程培训机构排名前十
  • 安全狗网站白名单指什么流程图在线制作免费
  • 做网站和做小程序有什么不同网络结构图怎么画
  • 网站开发师职责网站建设的公司这个
  • 企业为什么要自助建站世界互联网峰会2022
  • 西安seo关键词排名湛江seo推广外包
  • 电子商务网站体系结构有哪些?wordpress主题 四亩地
  • 名创 网站建设企业网站seo案例分析
  • 网站建设阿里云搭建个人网站广西网站建设营销公司
  • 营销型企业网站的建设步骤seo建站是什么意思
  • 网站错位vs做网站怎样添加图片
  • 网络宣传渠道优化英语
  • 青岛李村网站设计公司ui界面交互设计
  • 企业网站建设的四大因素婚纱网页设计素材
  • 湛江免费建站模板成都网站建设网站制作公司
  • 鸭梨网站建设舟山做网站公司
  • 培训机构网站建设推广wordpress 删除后台登录logo
  • 温州 建网站娄底市网站建设
  • 网站开发设计制作合同怎样做校园网站推广
  • 应用网站乘客电梯做推广的网站
  • 网站版面特点wordpress文章搜索排序
  • 外贸网站有哪些?厦门专业网站设计公
  • 响应式网站注意事项怎么做代理