shiro

  • p6_322473
    了解作者
  • 164.8KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-11 03:30
    上传日期
NULL 博文链接:https://xls9577087.iteye.com/blog/2164352
crm.zip
内容介绍
package com.cmb.crm.modules.sys.security; import java.io.Serializable; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.PostConstruct; import org.apache.commons.lang3.StringUtils; 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.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.cache.Cache; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.SimplePrincipalCollection; import org.apache.shiro.util.ByteSource; import org.springframework.stereotype.Service; import com.cmb.crm.common.utils.Encodes; import com.cmb.crm.modules.sys.model.Menu; import com.cmb.crm.modules.sys.model.User; import com.cmb.crm.modules.sys.service.SystemService; import com.cmb.crm.modules.sys.util.UserUtils; /** * 系统安全认证实现类 * * @author xiongliangsheng * @version 2014-12-08 */ @Service("crmAuthorizingRealm") public class SystemAuthorizingRealm extends AuthorizingRealm { public static final int SALT_SIZE = 16; public static final int HASH_INTERATIONS = 1024; public SystemAuthorizingRealm() { super(); } /** * 认证回调函数, 登录时调用 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { /* 这里编写认证代码 */ UsernamePasswordToken token = (UsernamePasswordToken) authcToken; String userName = token.getUsername(); User user = new User(); // User user = getSystemService().getUserByLoginName(userName); user.setId("001"); user.setUsername(userName); user.setPassword(SystemService.entryptPassword("admin")); if (user != null) { byte[] salt = Encodes.decodeHex(user.getPassword().substring(0,16)); return new SimpleAuthenticationInfo(new Principal(user), user.getPassword().substring(16), ByteSource.Util.bytes(salt), getName()); } else { return null; } } /** * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { /* 这里编写授权代码 */ Set<String> roleNames = new HashSet<String>(); Set<String> permissions = new HashSet<String>(); Principal principal = (Principal)principals.getPrimaryPrincipal(); //User user = getSystemService().getUserByLoginName(principal.getId()); User user = new User(); user.setId("001"); if ("superAdmin".equalsIgnoreCase(principal.getUserName())) { roleNames.add("superAdmin"); permissions.add("superAdmin"); permissions.add("main"); permissions.add("admin"); } else { roleNames.add("admin"); permissions.add("admin"); permissions.add("main"); } if (user != null) { UserUtils.putCache(UserUtils.CACHE_USER, user); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.setStringPermissions(permissions); info.addRoles(roleNames); /*List<Menu> list = UserUtils.getMenuList(); for (Menu menu : list){ if (StringUtils.isNotBlank(menu.getPermission())){ // 添加基于Permission的权限信息 for (String permission : StringUtils.split(menu.getPermission(),",")){ info.addStringPermission(permission); } } }*/ // 更新登录IP和时间 //getSystemService().updateUserLoginInfo(user.getId()); return info; } else { return null; } } /** * 设定密码校验的Hash算法与迭代次数 */ @PostConstruct public void initCredentialsMatcher() { HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(SystemService.HASH_ALGORITHM); matcher.setHashIterations(SystemService.HASH_INTERATIONS); setCredentialsMatcher(matcher); } /** * 清空用户关联权限认证,待下次使用时重新加载 */ public void clearCachedAuthorizationInfo(String principal) { SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName()); clearCachedAuthorizationInfo(principals); } /** * 清空所有关联认证 */ public void clearAllCachedAuthorizationInfo() { Cache<Object, AuthorizationInfo> cache = getAuthorizationCache(); if (cache != null) { for (Object key : cache.keys()) { cache.remove(key); } } } /** * 获取系统业务对象 */ /* * public SystemService getSystemService() { if (systemService == null){ * systemService = SpringContextHolder.getBean(SystemService.class); } * return systemService; } */ /** * 授权用户信息 */ public static class Principal implements Serializable { private static final long serialVersionUID = 1L; private String id; private String userName; private Map<String, Object> cacheMap; public Principal(User user) { this.id = user.getId(); this.userName = user.getUsername(); } public String getId() { return id; } public String getUserName() { return userName; } public Map<String, Object> getCacheMap() { if (cacheMap==null){ cacheMap = new HashMap<String, Object>(); } return cacheMap; } } }
评论
    相关推荐
    • nodejs http-server开启https的证书
      https证书文件,nodejs http-server开启https的证书,可以直接在本地启动https的协议,方便部署和使用。
    • http https 切换
      http https切换代码,能够实现网站在传输信息的过程中不易遭人盗取
    • Http2Https-crx插件
      将书签中的http转换为https 害怕用https书签替换旧的http书签? 随着许多网站迁移到https,旧的http书签使星标图像为空。 搜索并转换为https! ****************************************************** ***********...
    • levelweb:通过 httphttps 的 LevelDB
      Leveldb 通过httphttps 。 用法 var http = require ( 'http' ) ; var levelweb = require ( 'levelweb' ) ; var db = require ( 'level' ) ( './db' ) ; var opts = { prefix : '/' } ; // // like this... // ...
    • LocomotiveCMS https to http-crx插件
      语言:English 将登录网址从https替换为http以进行开发 此扩展程序供使用机车cms开发应用程序时供个人使用。 为了易于在开发模式下使用,此扩展程序将https登录网址替换为http
    • timed-out:超时HTTPHTTPS请求
      超时HTTP / HTTPS请求 ESOCKETTIMEDOUT ClientRequest时,发出错误code属性等于ETIMEDOUT或ESOCKETTIMEDOUT错误对象。 用法 import http from 'node:http' ; import timedOut from 'timed-out' ; const request = ...
    • http:https的镜像
      框架HTTP
    • https镜像
      scp镜 SCP基金会部分界面https //
    • CookieIsolator:分离 HTTPHTTPS cookie
      松散隔离:HTTP cookie 只能在 HTTP 通道中发送,而 HTTPS cookie 可以在 HTTP(不带安全标志)和 HTTPS 通道中发送。 严格隔离:HTTP cookie 和 HTTPS cookie 只能分别在 HTTPHTTPS 通道中发送。 Ext Secure...
    • https协议
      NULL 博文链接:https://willwen.iteye.com/blog/1988199