1.普通用戶實現redis共享session
1.配置
#cache指定緩存類型 spring.cache.type=REDIS #data-redis spring.redis.database=15 //單節點配置 可擇庫 spring.redis.password= spring.redis.host=192.168.210.*** //單節點配置 spring.redis.port=6379 spring.redis.timeout=2000 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-wait=-1 spring.redis.jedis.pool.min-idle=0
#集群配置 配置后單節點失效 #spring.redis.cluster.nodes=192.168.43.**:7000,192.168.43.**:7001,192.168.43.**:7000,192.168.43.**:7001,192.168.43.**:7000,192.168.43.**:7001 #spring.redis.cluster.max-redirects=3
#主從節點配置 配置后單節點,集群配置都失效
#spring.redis.sentinel.master=mymaster
#spring.redis.sentinel.nodes=192.168.210.**\:26379
#session share unit MINUTES 指定session在redis中過期時間
session.timeout=3
#cacheTimeOut unit MINUTES 指定權限信息在redis中過期時間
cache.timeout=12
2.開啟緩存
@SpringBootApplication @EnableCaching //開啟緩存 public class OneserviceManagerApplication { public static void main(String[] args) { SpringApplication.run(OneserviceManagerApplication.class, args); } }
3.編寫RedisConfig.java配置類,主要作用是對象序列化
package com.ch.evaluation.config.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig extends CachingConfigurerSupport { @Autowired private RedisConnectionFactory redisConnectionFactory; /** * 獲取RedisTemplate對象,處理Redis數據,並且進行最佳序列化 * @return */ @Bean(name="redisTemplate") public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); //手動序列化 JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setValueSerializer(jdkSerializationRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setHashValueSerializer(jdkSerializationRedisSerializer); //連接Redis template.setConnectionFactory(redisConnectionFactory); template.afterPropertiesSet(); return template; } }
4.RedisSessionDao的自定義實現(session的緩存處理)注意修改redis緩存的項目名
package com.ch.evaluation.auth.shiro.cas;
import com.ch.evaluation.common.util.PropertityUtil;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @description:SessionDao自定義實現
* @author: wangwei
* @date: 2018年11月27日
*/
@SuppressWarnings("all")
public class RedisSessionDao extends AbstractSessionDAO {
private final static String PREFIX="evaluation:shiro_redis_session:";
private static Logger logger = LoggerFactory.getLogger(RedisSessionDao.class);
private RedisTemplate redisTpl;
@Override
public void update(Session session) throws UnknownSessionException {
if (session==null || session.getId() == null){
logger.error("redis update session error:session or session id is null");
return;
}
try {
redisTpl.opsForValue().set(PREFIX+session.getId().toString(), session, PropertityUtil.getPropertity("session.timeout"), TimeUnit.MINUTES);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new UnknownSessionException(e);
}
}
@Override
public void delete(Session session) {
if (session==null || session.getId() == null){
logger.error("redis delete session error:session or session id is null");
return;
}
try {
redisTpl.delete(PREFIX+session.getId().toString());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
@Override
public Collection<Session> getActiveSessions() {
Set<Session> sessions = new HashSet<Session>();
Set keys = redisTpl.keys(PREFIX+"*");
for(Object key : keys){
Session session=(Session) redisTpl.opsForValue().get(key);
sessions.add(session);
}
return sessions;
}
@Override
protected Serializable doCreate(Session session) {
if (session==null){
logger.error("redis create session error:session is null");
return null;
}
Serializable sessionId = generateSessionId(session);
assignSessionId(session, sessionId);
redisTpl.opsForValue().set(PREFIX+sessionId.toString(), session, PropertityUtil.getPropertity("session.timeout"), TimeUnit.MINUTES);
return sessionId;
}
@Override
protected Session doReadSession(Serializable sessionId) {
if (sessionId == null){
logger.error("redis read session error:sessionId is null");
return null;
}
Session session = null;
try {
session = (Session) redisTpl.opsForValue().get(PREFIX+sessionId);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return session;
}
public void setRedisTpl(RedisTemplate redisTpl) {
this.redisTpl = redisTpl;
}
}
5.上面用到了一個工具類加載配置文件
package com.ch.evaluation.common.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertityUtil { public static int getPropertity(String key){ Properties properties = new Properties(); ClassLoader load = PropertityUtil.class.getClassLoader(); InputStream is = load.getResourceAsStream("application.properties"); try { properties.load(is); String value = properties.getProperty(key); int val = 0; if(value!=null){ val = Integer.parseInt(value); } return val; } catch (IOException e) { e.printStackTrace(); } return 0; } }
7..RedisCache的自定義實現(對權限和認證信息的緩存處理)注意修改redis緩存的項目名
package com.ch.evaluation.common.redis; import com.ch.evaluation.common.util.PropertityUtil; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.springframework.data.redis.core.RedisTemplate; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Set; import java.util.concurrent.TimeUnit; /** * Redis緩存類 * Created by 005803 on 2017/10/12. */ public class RedisCache<K, V> implements Cache<K, V> { private RedisTemplate redisTemplate; private final static long SUPER_AMDIN_TICKET_EXPARE_TIME =3; private static final String PREFIX = "evaluation:shiro_redis_cache:"; private static final String SUPER_TICKET_KEY = "evaluation:super_ticket:"; public RedisCache(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } @Override public V get(K key) throws CacheException { return (V) redisTemplate.opsForValue().get(PREFIX + key); } @Override public V put(K key, V value) throws CacheException { redisTemplate.opsForValue().set(PREFIX + key, value, PropertityUtil.getPropertity("cache.timeout"), TimeUnit.MINUTES); return value; } @Override public V remove(K key) throws CacheException { Object value = redisTemplate.opsForValue().get(PREFIX + key); redisTemplate.delete(PREFIX + key); return (V) value; } @Override public void clear() throws CacheException { redisTemplate.delete(keys()); } @Override public int size() { return keys().size(); } @Override public Set<K> keys() { Set keys = redisTemplate.keys(PREFIX + "*"); return keys != null ? keys : Collections.<K>emptySet(); } @Override public Collection<V> values() { Set<K> keys = keys(); Collection<V> c = new HashSet<>(); for (K key : keys) { c.add((V) redisTemplate.opsForValue().get(key)); } return c; } public V putSuper(K key, V value) throws CacheException { redisTemplate.opsForHash().put(SUPER_TICKET_KEY,key,value); redisTemplate.expire(SUPER_TICKET_KEY,SUPER_AMDIN_TICKET_EXPARE_TIME,TimeUnit.MINUTES); return value; } public Set<V> getAllSuperKeys() throws CacheException { return redisTemplate.opsForHash().keys(SUPER_TICKET_KEY); } public V getSuper(K key) throws CacheException { return (V) redisTemplate.opsForHash().get(SUPER_TICKET_KEY,key); } public void deleteSuper(K key) throws CacheException { redisTemplate.opsForHash().delete(SUPER_TICKET_KEY,key); } public boolean hasKey(K key) throws CacheException { return redisTemplate.opsForHash().hasKey(SUPER_TICKET_KEY,key); } }
8..Redis緩存管理器的配置RedisCacheManager.java
package com.ch.evaluation.common.redis; import org.apache.shiro.cache.Cache; import org.apache.shiro.cache.CacheException; import org.apache.shiro.cache.CacheManager; import org.springframework.data.redis.core.RedisTemplate; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * Redis緩存管理器 * Created by wangwei on 2018/10/19. */ public class RedisCacheManager implements CacheManager { private RedisTemplate redisTemplate; private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<>(); public void setRedisTemplate(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } @Override public <K, V> Cache<K, V> getCache(String name) throws CacheException { Cache cache = caches.get(name); if (cache == null) { cache = new RedisCache(redisTemplate); caches.put(name, cache); } return cache; } }
9.在你自定義的shiro的realm中重寫key的策略
public class ExtendCasRealm extends CasRealm { private static Logger LOGGER = LoggerFactory.getLogger(ExtendCasRealm.class); @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { ...................... } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { ........................ } .................... .................. @Override protected Object getAuthorizationCacheKey(PrincipalCollection principals) { return principals.getPrimaryPrincipal() + ":authorization"; } @Override protected Object getAuthenticationCacheKey(PrincipalCollection principals) { return principals.getPrimaryPrincipal() + ":authentication"; } @Override protected Object getAuthenticationCacheKey(AuthenticationToken token) { return token.getPrincipal() + ":authentication"; } }
10.基本上配置以上信息就可以用了,值得注意的是要在ShiroCasConfig中配置這些Bean的關聯關系,記得session的獲取方式有兩種,一種是servlet的session一種是shiro默認的session管理器DefaultWebSessionManager ,我們要記得注入DefaultWebSessionManager 管理器,不然程序執行過程中可能會默認執行isServletContainerSessions方法導致拋出一個session類型的異常
貼一下ShiroCasConfig配置
package com.ch.evaluation.config.shirocas; import com.ch.evaluation.auth.shiro.cas.ExtendCasRealm; import com.ch.evaluation.auth.shiro.cas.RedisSessionDao; import com.ch.evaluation.auth.shiro.filter.ExtendAuthorizationFilter; import com.ch.evaluation.auth.shiro.filter.ExtendCasFilter; import com.ch.evaluation.auth.shiro.filter.ExtendLogoutFilter; import com.ch.evaluation.auth.shiro.service.IAuthorizationService; import com.ch.evaluation.common.constants.WebConstants; import com.ch.evaluation.common.redis.RedisCacheManager; import org.apache.shiro.cache.MemoryConstrainedCacheManager; import org.apache.shiro.cas.CasSubjectFactory; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; import org.jasig.cas.client.session.SingleSignOutFilter; import org.jasig.cas.client.session.SingleSignOutHttpSessionListener; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.filter.DelegatingFilterProxy; import javax.servlet.Filter; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; /** * Created by sunyong - 20170906 */ @Configuration public class ShiroCasConfig { @Value("${cas.server.url}") private String casServerUrl; @Value("${shiro.cas-server}") private String casServerUrlPrefix; @Value("${shiro.server}") private String shiroServerUrlPrefix; private static final String CAS_FILTER_NAME = "casFilter"; private static final String SHIRO_FILTER_NAME = "shiroFilter"; private static final String AUTH_FILTER_NAME = "authFilter"; private static final String LOGOUT_FILTER_NAME = "logoutFilter"; /** * 注冊DelegatingFilterProxy(Shiro) */ @Bean public FilterRegistrationBean<DelegatingFilterProxy> filterRegistrationBean() { FilterRegistrationBean<DelegatingFilterProxy> filterRegistration = new FilterRegistrationBean<DelegatingFilterProxy>(); filterRegistration.setFilter(new DelegatingFilterProxy(SHIRO_FILTER_NAME)); filterRegistration.addInitParameter("targetFilterLifecycle", "true"); filterRegistration.setEnabled(true); filterRegistration.addUrlPatterns("/*"); return filterRegistration; } /** * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor( DefaultWebSecurityManager securityManager) { AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } /** * 會話管理器 * @auth 011336 * @DATE 2018/10/15 * @return */ @Bean(name = "sessionManager") public DefaultWebSessionManager getDefaultWebSessionManager(RedisSessionDao sessionDAO, RedisCacheManager redisCacheManager) { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); //sessionManager.setGlobalSessionTimeout(sessionTimeout); sessionManager.setDeleteInvalidSessions(true); //sessionManager.setSessionValidationSchedulerEnabled(true); sessionManager.setSessionDAO(sessionDAO); sessionManager.setCacheManager(redisCacheManager); // TODO simpleCookie return sessionManager; } /** * 實例化SecurityManager,該類是shiro的核心類 * * @return */ @Bean public DefaultWebSecurityManager getDefaultWebSecurityManager(ExtendCasRealm extendCasRealm, DefaultWebSessionManager sessionManager, RedisCacheManager redisCacheManager) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(extendCasRealm); securityManager.setCacheManager(redisCacheManager); securityManager.setSessionManager(sessionManager); securityManager.setSubjectFactory(new CasSubjectFactory()); return securityManager; } /** * RedisSessionDao * @auth 011336 * @DATE 2018/10/15 * @return */ @Bean public RedisSessionDao getRedisSessionDao(RedisTemplate redisTemplate) { RedisSessionDao sessionDAO = new RedisSessionDao(); sessionDAO.setRedisTpl(redisTemplate); return sessionDAO; } /** * redisCacheManager * @auth 011336 * @DATE 2018/10/15 * @return */ @Bean public RedisCacheManager getRedisCacheManager(RedisTemplate redisTemplate) { RedisCacheManager redisCacheManager = new RedisCacheManager(); redisCacheManager.setRedisTemplate(redisTemplate); return redisCacheManager; } /** * 配置Realm,由於我們使用的是CasRealm,所以已經集成了單點登錄的功能 * * @param authorizationService * @return */ @Bean public ExtendCasRealm getExtendCasRealm(IAuthorizationService authorizationService, RedisCacheManager redisCacheManager ){ ExtendCasRealm extendCasRealm = new ExtendCasRealm(); extendCasRealm.setAuthorizationService(authorizationService); // cas登錄服務器地址前綴 extendCasRealm.setCasServerUrlPrefix(casServerUrlPrefix); // 客戶端回調地址,登錄成功后的跳轉地址(自己的服務地址) extendCasRealm.setCasService(shiroServerUrlPrefix + WebConstants.CAS_FILTER_URI); extendCasRealm.setCachingEnabled(true); extendCasRealm.setAuthenticationCachingEnabled(true); extendCasRealm.setAuthenticationCacheName("authenticationCache"); extendCasRealm.setAuthorizationCachingEnabled(true); extendCasRealm.setAuthorizationCacheName("authorizationCache"); extendCasRealm.setCacheManager(redisCacheManager); return extendCasRealm; } /** * 注冊單點登出的listener * * @return */ @Bean @Order(Ordered.HIGHEST_PRECEDENCE) // 優先級需要高於Cas的Filter public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> singleSignOutHttpSessionListener() { ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> bean = new ServletListenerRegistrationBean<SingleSignOutHttpSessionListener>(); bean.setListener(new SingleSignOutHttpSessionListener()); bean.setEnabled(true); return bean; } /** * 注冊單點登出filter * * @return */ @Bean public FilterRegistrationBean<SingleSignOutFilter> singleSignOutFilter() { FilterRegistrationBean<SingleSignOutFilter> bean = new FilterRegistrationBean<SingleSignOutFilter>(); bean.setName("singleSignOutFilter"); bean.setFilter(new SingleSignOutFilter()); bean.addUrlPatterns("/*"); bean.setEnabled(true); return bean; } /** * CAS過濾器 * * @return */ //@Bean(name = CAS_FILTER_NAME) public ExtendCasFilter getExtendCasFilter() { ExtendCasFilter casFilter = new ExtendCasFilter(); casFilter.setName(CAS_FILTER_NAME); casFilter.setEnabled(true); // String loginUrl = casServerUrl + "/login?service=" + shiroServerUrlPrefix + CAS_FILTER_URI; casFilter.setFailureUrl("/error/casfailure"); casFilter.setExtendFailureUrl("/error/casfailure"); // 由於原failuserUrl為私有字段,在擴展類中不能獲取到值 return casFilter; } /** * extAuth Filter */ //@Bean(name = AUTH_FILTER_NAME) public ExtendAuthorizationFilter getExtendAuthorizationFilter( IAuthorizationService authorizationService) { ExtendAuthorizationFilter extAuthFilter = new ExtendAuthorizationFilter(); extAuthFilter.setName(AUTH_FILTER_NAME); extAuthFilter.setEnabled(true); extAuthFilter.setAuthorizationService(authorizationService); return extAuthFilter; } /** * extLogout Filter */ //@Bean(name = LOGOUT_FILTER_NAME) public ExtendLogoutFilter getExtendLogoutFilter(IAuthorizationService authorizationService) { ExtendLogoutFilter extLogoutFilter = new ExtendLogoutFilter(); extLogoutFilter.setName(LOGOUT_FILTER_NAME); extLogoutFilter.setEnabled(true); extLogoutFilter.setAuthorizationService(authorizationService); extLogoutFilter.setRedirectUrl(casServerUrl + "/logout?service=" + shiroServerUrlPrefix); return extLogoutFilter; } /** * 使用工廠模式,創建並初始化ShiroFilter * * @param securityManager * @param authorizationService * @return */ @Bean(name = SHIRO_FILTER_NAME) public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager, IAuthorizationService authorizationService) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); String loginUrl = casServerUrl + "/login?service=" + shiroServerUrlPrefix + WebConstants.CAS_FILTER_URI; shiroFilterFactoryBean.setLoginUrl(loginUrl); shiroFilterFactoryBean.setSuccessUrl("/"); shiroFilterFactoryBean.setUnauthorizedUrl("/error/unauthorized"); Map<String, Filter> filters = new HashMap<>(); filters.put(CAS_FILTER_NAME, getExtendCasFilter()); filters.put(LOGOUT_FILTER_NAME, getExtendLogoutFilter(authorizationService)); filters.put(AUTH_FILTER_NAME, getExtendAuthorizationFilter(authorizationService)); shiroFilterFactoryBean.setFilters(filters); loadShiroFilterChain(shiroFilterFactoryBean); return shiroFilterFactoryBean; } private void loadShiroFilterChain(ShiroFilterFactoryBean shiroFilterFactoryBean) { Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); filterChainDefinitionMap.put(WebConstants.CAS_FILTER_URI, CAS_FILTER_NAME); filterChainDefinitionMap.put("/logout", LOGOUT_FILTER_NAME); filterChainDefinitionMap.put("/static/**", "anon"); filterChainDefinitionMap.put("/css/**", "anon"); filterChainDefinitionMap.put("/front/**", "anon"); filterChainDefinitionMap.put("/img/**", "anon"); filterChainDefinitionMap.put("/js/**", "anon"); filterChainDefinitionMap.put("/plugin/**", "anon"); filterChainDefinitionMap.put("/home/**", "anon"); filterChainDefinitionMap.put("/super", "anon"); filterChainDefinitionMap.put("/interface/**", "anon"); filterChainDefinitionMap.put("/super/login", "anon"); filterChainDefinitionMap.put("/error/**", "anon"); filterChainDefinitionMap.put("/**", AUTH_FILTER_NAME); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); } }
二:超管redis的session共享
1..把原來的SuperAdminTickeUtils更名為SuperAdminTicketManager,或者刪除替換也行。記得把其他用到這個類的地方改一下就行,不詳細說改了啥了。
package com.ch.evaluation.auth.shiro; import com.ch.evaluation.auth.shiro.entity.AuthorizationUser; import com.ch.evaluation.common.redis.RedisCache; import com.ch.evaluation.common.redis.RedisCacheManager; import org.jasig.cas.client.authentication.AttributePrincipalImpl; import org.jasig.cas.client.validation.Assertion; import org.jasig.cas.client.validation.AssertionImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.*; @Component public class SuperAdminTicketManager { private static Logger LOGGER = LoggerFactory.getLogger(SuperAdminTicketManager.class); private final static String SUPER_AMDIN_TICKET_SUFFIX = ".superadmin.com"; private final static long SUPER_AMDIN_TICKET_TIME = 1000 * 60 * 3; private final static String SUPER_TICKET = "superTicket"; @Autowired private RedisCacheManager redisCacheManager; public String putTicket(AuthorizationUser user) { RedisCache cache = (RedisCache)redisCacheManager.getCache(SUPER_TICKET); String ticket = getTicket(); cache.putSuper(ticket,user); return ticket; } public boolean validTiket(String ticket) { RedisCache cache = (RedisCache)redisCacheManager.getCache(SUPER_TICKET); clearTicketMap(cache); return cache.hasKey(ticket); } public boolean endsWith(String ticket) { return ticket.endsWith(SUPER_AMDIN_TICKET_SUFFIX); } private static String getTicket() { return UUID.randomUUID() + SUPER_AMDIN_TICKET_SUFFIX; } private void clearTicketMap(RedisCache cache) { Long currentTime = new Date().getTime(); Set<String> keys= cache.getAllSuperKeys(); for (Object key:keys) { AuthorizationUser user = (AuthorizationUser)cache.getSuper(key); if((currentTime - user.getTime()) > SUPER_AMDIN_TICKET_TIME){ LOGGER.info("super.ticket has expired and delete from redis!"); cache.deleteSuper(key); } } } public final Assertion getSuperAdminAssertion(String ticket) { Assertion assertion = null; final Map<String, Object> attributes = new HashMap<String, Object>(); RedisCache cache = (RedisCache)redisCacheManager.getCache(SUPER_TICKET); AuthorizationUser user = (AuthorizationUser)cache.getSuper(ticket); if (null != user) { attributes.put("user_id", user.getUserId()); attributes.put("user_name", user.getUserName()); attributes.put("password", user.getPassword()); assertion = new AssertionImpl(new AttributePrincipalImpl(user.getUserAccount(), attributes)); } cache.deleteSuper(ticket); return assertion; } }
2.修改之前用到這個類的地方
2.1 修改ExtendCasRealm 新增一處 改三處
@Autowired
private SuperAdminTicketManager superAdminTicketManager;
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
CasToken casToken = (CasToken) token;
if (token == null) {
return null;
}
String ticket = (String)casToken.getCredentials();
if (!StringUtils.hasText(ticket)) {
return null;
}
boolean superAdminFlag = superAdminTicketManager.endsWith(ticket);
...........
private Assertion getSuperAdminAssertion(CasToken casToken, String ticket)
throws CasAuthenticationException {
if (!superAdminTicketManager.validTiket(ticket)) {
throw new CasAuthenticationException("Invalid super ticket [" + ticket + "]");
}
Assertion casAssertion = superAdminTicketManager.getSuperAdminAssertion(ticket);
return casAssertion;
}
2.2SuperAdminServiceImpl 中新增一處 改一處
@Autowired private SuperAdminTicketManager superAdminTicketManager;
....
String ticket = superAdminTicketManager.putTicket(user);
return ticket;
}
}
3.序列化:實現接口后鼠標放在類上alt+Enter就可以生成uid
public class AuthorizationUser implements Serializable { private static final long serialVersionUID = -5556165398740497973L;
三:超管登錄密碼加密
1.引入js文件夾到plugin
2.layout-superlogin.html中引入JS
<script class="custom-script" src="../../static/plugin/crypto-js/crypto-js.js" th:src="@{/plugin/crypto-js/crypto-js.js}"></script> <script class="custom-script" src="../../static/plugin/crypto-js/core.js" th:src="@{/plugin/crypto-js/core.js}"></script> <script class="custom-script" src="../../static/plugin/crypto-js/aes.js" th:src="@{/plugin/crypto-js/aes.js}"></script>
3.編寫superlogin.js文件
var key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六進制數作為密鑰 var iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); //十六位十六進制數作為密鑰偏移量 //解密方法 function Decrypt(word) { var encryptedHexStr = CryptoJS.enc.Hex.parse(word); var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); } //加密方法 function Encrypt(word) { var srcs = CryptoJS.enc.Utf8.parse(word); var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString().toUpperCase(); } function superLogin(){ var passWord = $("#signin-superamdin-password").val(); if(passWord==null || passWord==""){ passWord =""; }else{ passWord = passWord.trim(); } var prefix = getTimeStr("prefix"); var suffix = getTimeStr("suffix"); passWord = prefix+passWord+suffix var aesPassWord = Encrypt(passWord); $("#submit-superamdin-password").val(aesPassWord); return true; } function getTimeStr(flag){ var myDate = new Date(); var year = myDate.getFullYear(); //獲取完整的年份(4位,1970-????) var month = myDate.getMonth()+1; //獲取當前月份(0-11,0代表1月) month = month > 9 ? month : "0"+month; var day = myDate.getDate(); //獲取當前日(1-31) day = day > 9 ? day : "0"+day; var hours = myDate.getHours(); //獲取當前小時數(0-23) hours = hours > 9 ? hours : "0"+hours; var minutes = myDate.getMinutes(); //獲取當前分鍾數(0-59) minutes = minutes > 9 ? minutes : "0"+minutes; var seconds = myDate.getSeconds(); //獲取當前秒數(0-59) seconds = seconds > 9 ? seconds : "0"+seconds; if(flag=="prefix"){ return ""+year+month+day }else{ return ""+hours+minutes+seconds } }
3.1:替換html部分的form部分
4.可直接替換superloginController.java 詳情如下
4.1:校驗是否超時,獲取時間差
public boolean checkLogionTime(String rangeTime){ String strDate = rangeTime; //注意:SimpleDateFormat構造函數的樣式與strDate的樣式必須相符 SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上時間 //必須捕獲異常 Date date= null; try { date = simpleDateFormat.parse(strDate); } catch (ParseException e) { e.printStackTrace(); } long min = getDatePoor(new Date(),date); if(min>=loginTimeOut) { return false; }else{ return true; } } //計算時間差 差多少分鍾 絕對值 public static long getDatePoor(Date endDate, Date loginDate) { long nd = 1000 * 24 * 60 * 60; long nh = 1000 * 60 * 60; long nm = 1000 * 60; // long ns = 1000; // 獲得兩個時間的毫秒時間差異 long diff = endDate.getTime() - loginDate.getTime(); // 計算差多少分鍾 long min = diff % nd % nh / nm; return Math.abs(min); }
4.2:驗證之前解密 直接加在原來的步驟中就好
public String login(ModelMap modal, String superAdminUsername, String superAdminPassword, HttpServletRequest request, HttpServletResponse response) { if (StringUtils.isNotBlank(superAdminUsername) && StringUtils.isNotBlank(superAdminPassword)) { try { String str = AesUtil.desEncrypt(superAdminPassword); superAdminPassword = str.substring(8, str.length() - 6); String rangeTime = str.substring(0,8)+str.substring(str.length()-6); boolean b = checkLogionTime(rangeTime); if(!b) { modal.put(ErrorConstants.ERROR_MESSAGE, ErrorConstants.ERROR_SUPERADMIN_003); return "views/superLoginPage"; } } catch (Exception e) { LOGGER.error("decrypt applicationMetadataId failed", e); } SuperAdmin superAdmin = new SuperAdmin(); superAdmin.setUsername(superAdminUsername.trim());
有個超時時間的設置在propertity中
# unit minutes super.login.timeOut=5
5.后台要引入一個解密的Java工具類,ErrorConstants中加一個錯誤提示信息
public final static String ERROR_SUPERADMIN_003 = "登陸超時,不安全的請求!"; // 判斷登錄請求超過一定時間為不安全請求
完事!
四:單點登錄ticket驗證地址變更
1.ShiroCasConfig中假如如下配置
@Value("${cas.server.url}") private String casServerUrl;
替換如下內容
//@Bean(name = LOGOUT_FILTER_NAME) public ExtendLogoutFilter getExtendLogoutFilter(IAuthorizationService authorizationService) { ExtendLogoutFilter extLogoutFilter = new ExtendLogoutFilter(); extLogoutFilter.setName(LOGOUT_FILTER_NAME); extLogoutFilter.setEnabled(true); extLogoutFilter.setAuthorizationService(authorizationService); extLogoutFilter.setRedirectUrl(casServerUrl + "/logout?service=" + shiroServerUrlPrefix); return extLogoutFilter; }
@Bean(name = SHIRO_FILTER_NAME)
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager,
IAuthorizationService authorizationService) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
String loginUrl = casServerUrl + "/login?service=" + shiroServerUrlPrefix + WebConstants.CAS_FILTER_URI;
shiroFilterFactoryBean.setLoginUrl(loginUrl);
完事!