Shiro+Cas微服务化及前后端完全分离微服务化及前后端完全分离
主要为大家详细介绍了Shiro+Cas微服务化及前后端完全分离,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Shiro Cas微服务化及前后端完全分离,供大家参考,具体内容如下
shiro+cas微服务化笔记
1.Spring Boot 配置配置
有如下两个配置文件:ShiroBaseConfig.java
import lombok.extern.log4j.Log4j;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.cas.CasFilter;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <p>
* Description: shiro权限管理模块conf
*
* @author Dean.Hwang
* @date 17/5/18
*/
@Configuration
@Log4j
public class ShiroBaseConfiguration {
@Value("${cas.server.url.prefix}")
private String casPrefix;
@Value("${cas.service}")
private String casService;
/**
* 会话Cookie模板
*
* @return
*/
@Bean
public SimpleCookie sessionIdCookie() {
SimpleCookie simpleCookie = new SimpleCookie("sid");
simpleCookie.setHttpOnly(true);
simpleCookie.setMaxAge(1800000);
return simpleCookie;
}
/**
* 会话Cookie模板
*
* @return
*/
@Bean
public SimpleCookie rememberCookie() {
SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
simpleCookie.setHttpOnly(true);
simpleCookie.setMaxAge(2592000);//30天
return simpleCookie;
}
/**
* rememberMe 管理器
*
* @return
*/
@Bean
public CookieRememberMeManager rememberMeManager(SimpleCookie rememberCookie) {
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
cookieRememberMeManager.setCipherKey(Base64.decode(""));// rememberMe cookie加密的密钥 建议每个项目都不一样 默认AES算法 密钥长度(128 256 512 位)
cookieRememberMeManager.setCookie(rememberCookie);
return cookieRememberMeManager;
}
/**
* 会话DAO
*
* @return
*/
@Bean
public MemorySessionDAO sessionDAO() {
return new MemorySessionDAO();
}
@Bean
public CacheManager shiroCacheManager() {
return new MemoryConstrainedCacheManager();
}
@Bean
public KryCasRealm casRealm(CacheManager shiroCacheManager) {
return new KryCasRealm(casPrefix, casService, shiroCacheManager);
}
@Bean
public CasFilter casFilter() {
CasFilter casFilter = new CasFilter();
casFilter.setEnabled(true);
casFilter.setName("casFilter");
casFilter.setFailureUrl("/authority/casFailure");
return casFilter;
}
}
下面ShiroManagerConfiguration.java 文件
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cas.CasFilter;
import org.apache.shiro.cas.CasSubjectFactory;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.ServletContainerSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
评论1
最新资源