shiro使用started依赖;
This commit is contained in:
parent
0dfbee487a
commit
b59d4219d2
11
pom.xml
11
pom.xml
|
|
@ -64,14 +64,7 @@
|
|||
<!--Shiro核心框架 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-core</artifactId>
|
||||
<version>${shiro.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Shiro使用Srping框架 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-spring</artifactId>
|
||||
<artifactId>shiro-spring-boot-starter</artifactId>
|
||||
<version>${shiro.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
|
@ -81,7 +74,7 @@
|
|||
<artifactId>shiro-ehcache</artifactId>
|
||||
<version>${shiro.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- thymeleaf模板引擎和shiro框架的整合 -->
|
||||
<dependency>
|
||||
<groupId>com.github.theborakompanioni</groupId>
|
||||
|
|
|
|||
|
|
@ -93,6 +93,8 @@ spring:
|
|||
max-idle: 8
|
||||
min-idle: 2
|
||||
max-wait: -1
|
||||
application:
|
||||
name: framework
|
||||
# Shiro
|
||||
shiro:
|
||||
user:
|
||||
|
|
@ -146,10 +148,10 @@ swagger:
|
|||
cache:
|
||||
names:
|
||||
sys_dict_type:
|
||||
ttl: PT1M
|
||||
ttl: PT10M
|
||||
sys_user:
|
||||
ttl: PT1M
|
||||
ttl: PT10M
|
||||
sys_post:
|
||||
ttl: PT1M
|
||||
ttl: PT10M
|
||||
sys_role:
|
||||
ttl: PT1M
|
||||
ttl: PT10M
|
||||
|
|
@ -32,15 +32,19 @@
|
|||
<!--Shiro核心框架 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-core</artifactId>
|
||||
<artifactId>shiro-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Shiro使用EhCache缓存框架 -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-ehcache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.theborakompanioni</groupId>
|
||||
<artifactId>thymeleaf-extras-shiro</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 自定义验证注解 -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
|
|
|
|||
|
|
@ -48,12 +48,6 @@
|
|||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- Shiro使用Spring框架 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-spring</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- thymeleaf模板引擎和shiro框架的整合 -->
|
||||
<dependency>
|
||||
<groupId>com.github.theborakompanioni</groupId>
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import com.ruoyi.common.properties.CacheProperties;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
|
@ -27,7 +28,7 @@ import java.util.Map;
|
|||
@EnableCaching
|
||||
@Configuration
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class CacheConfig {
|
||||
public class CacheConfig extends CachingConfigurerSupport {
|
||||
|
||||
@Autowired
|
||||
private RedisConnectionFactory factory;
|
||||
|
|
@ -42,6 +43,7 @@ public class CacheConfig {
|
|||
}
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public CacheManager cacheManager() {
|
||||
RedisCacheConfiguration defaultConfiguration = RedisCacheConfiguration.defaultCacheConfig()
|
||||
.disableCachingNullValues()
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import java.io.InputStream;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.shiro.cache.ehcache.EhCacheManager;
|
||||
import org.apache.shiro.codec.Base64;
|
||||
|
|
@ -17,6 +18,7 @@ import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
|||
import org.apache.shiro.web.mgt.CookieRememberMeManager;
|
||||
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
||||
import org.apache.shiro.web.servlet.SimpleCookie;
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
|
@ -34,15 +36,16 @@ import com.ruoyi.framework.shiro.web.filter.sync.SyncOnlineSessionFilter;
|
|||
import com.ruoyi.framework.shiro.web.session.OnlineWebSessionManager;
|
||||
import com.ruoyi.framework.shiro.web.session.SpringSessionValidationScheduler;
|
||||
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* 权限配置加载
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
public class ShiroConfig
|
||||
{
|
||||
public class ShiroConfig {
|
||||
public static final String PREMISSION_STRING = "perms[\"{0}\"]";
|
||||
|
||||
/**
|
||||
|
|
@ -127,17 +130,13 @@ public class ShiroConfig
|
|||
* 缓存管理器 使用Ehcache实现
|
||||
*/
|
||||
@Bean
|
||||
public EhCacheManager getEhCacheManager()
|
||||
{
|
||||
public EhCacheManager getEhCacheManager() {
|
||||
net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.getCacheManager("ruoyi");
|
||||
EhCacheManager em = new EhCacheManager();
|
||||
if (StringUtils.isNull(cacheManager))
|
||||
{
|
||||
if (StringUtils.isNull(cacheManager)) {
|
||||
em.setCacheManager(new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream()));
|
||||
return em;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
em.setCacheManager(cacheManager);
|
||||
return em;
|
||||
}
|
||||
|
|
@ -146,24 +145,18 @@ public class ShiroConfig
|
|||
/**
|
||||
* 返回配置文件流 避免ehcache配置文件一直被占用,无法完全销毁项目重新部署
|
||||
*/
|
||||
protected InputStream getCacheManagerConfigFileInputStream()
|
||||
{
|
||||
protected InputStream getCacheManagerConfigFileInputStream() {
|
||||
String configFile = "classpath:ehcache/ehcache-shiro.xml";
|
||||
InputStream inputStream = null;
|
||||
try
|
||||
{
|
||||
try {
|
||||
inputStream = ResourceUtils.getInputStreamForPath(configFile);
|
||||
byte[] b = IOUtils.toByteArray(inputStream);
|
||||
InputStream in = new ByteArrayInputStream(b);
|
||||
return in;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
} catch (IOException e) {
|
||||
throw new ConfigurationException(
|
||||
"Unable to obtain input stream for cacheManagerConfigFile [" + configFile + "]", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
} finally {
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
}
|
||||
}
|
||||
|
|
@ -172,8 +165,7 @@ public class ShiroConfig
|
|||
* 自定义Realm
|
||||
*/
|
||||
@Bean
|
||||
public UserRealm userRealm(EhCacheManager cacheManager)
|
||||
{
|
||||
public UserRealm userRealm(EhCacheManager cacheManager) {
|
||||
UserRealm userRealm = new UserRealm();
|
||||
userRealm.setCacheManager(cacheManager);
|
||||
return userRealm;
|
||||
|
|
@ -183,8 +175,7 @@ public class ShiroConfig
|
|||
* 自定义sessionDAO会话
|
||||
*/
|
||||
@Bean
|
||||
public OnlineSessionDAO sessionDAO()
|
||||
{
|
||||
public OnlineSessionDAO sessionDAO() {
|
||||
OnlineSessionDAO sessionDAO = new OnlineSessionDAO();
|
||||
return sessionDAO;
|
||||
}
|
||||
|
|
@ -193,8 +184,7 @@ public class ShiroConfig
|
|||
* 自定义sessionFactory会话
|
||||
*/
|
||||
@Bean
|
||||
public OnlineSessionFactory sessionFactory()
|
||||
{
|
||||
public OnlineSessionFactory sessionFactory() {
|
||||
OnlineSessionFactory sessionFactory = new OnlineSessionFactory();
|
||||
return sessionFactory;
|
||||
}
|
||||
|
|
@ -203,8 +193,7 @@ public class ShiroConfig
|
|||
* 会话管理器
|
||||
*/
|
||||
@Bean
|
||||
public OnlineWebSessionManager sessionManager()
|
||||
{
|
||||
public OnlineWebSessionManager sessionManager() {
|
||||
OnlineWebSessionManager manager = new OnlineWebSessionManager();
|
||||
// 加入缓存管理器
|
||||
manager.setCacheManager(getEhCacheManager());
|
||||
|
|
@ -229,8 +218,7 @@ public class ShiroConfig
|
|||
* 安全管理器
|
||||
*/
|
||||
@Bean
|
||||
public SecurityManager securityManager(UserRealm userRealm)
|
||||
{
|
||||
public DefaultWebSecurityManager securityManager(UserRealm userRealm) {
|
||||
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
||||
// 设置realm.
|
||||
securityManager.setRealm(userRealm);
|
||||
|
|
@ -246,8 +234,7 @@ public class ShiroConfig
|
|||
/**
|
||||
* 退出过滤器
|
||||
*/
|
||||
public LogoutFilter logoutFilter()
|
||||
{
|
||||
public LogoutFilter logoutFilter() {
|
||||
LogoutFilter logoutFilter = new LogoutFilter();
|
||||
logoutFilter.setLoginUrl(loginUrl);
|
||||
return logoutFilter;
|
||||
|
|
@ -257,8 +244,7 @@ public class ShiroConfig
|
|||
* Shiro过滤器配置
|
||||
*/
|
||||
@Bean
|
||||
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager)
|
||||
{
|
||||
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
|
||||
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
||||
// Shiro的核心安全接口,这个属性是必须的
|
||||
shiroFilterFactoryBean.setSecurityManager(securityManager);
|
||||
|
|
@ -308,8 +294,7 @@ public class ShiroConfig
|
|||
* 自定义在线用户处理过滤器
|
||||
*/
|
||||
@Bean
|
||||
public OnlineSessionFilter onlineSessionFilter()
|
||||
{
|
||||
public OnlineSessionFilter onlineSessionFilter() {
|
||||
OnlineSessionFilter onlineSessionFilter = new OnlineSessionFilter();
|
||||
onlineSessionFilter.setLoginUrl(loginUrl);
|
||||
return onlineSessionFilter;
|
||||
|
|
@ -319,8 +304,7 @@ public class ShiroConfig
|
|||
* 自定义在线用户同步过滤器
|
||||
*/
|
||||
@Bean
|
||||
public SyncOnlineSessionFilter syncOnlineSessionFilter()
|
||||
{
|
||||
public SyncOnlineSessionFilter syncOnlineSessionFilter() {
|
||||
SyncOnlineSessionFilter syncOnlineSessionFilter = new SyncOnlineSessionFilter();
|
||||
return syncOnlineSessionFilter;
|
||||
}
|
||||
|
|
@ -329,8 +313,7 @@ public class ShiroConfig
|
|||
* 自定义验证码过滤器
|
||||
*/
|
||||
@Bean
|
||||
public CaptchaValidateFilter captchaValidateFilter()
|
||||
{
|
||||
public CaptchaValidateFilter captchaValidateFilter() {
|
||||
CaptchaValidateFilter captchaValidateFilter = new CaptchaValidateFilter();
|
||||
captchaValidateFilter.setCaptchaEnabled(captchaEnabled);
|
||||
captchaValidateFilter.setCaptchaType(captchaType);
|
||||
|
|
@ -340,8 +323,7 @@ public class ShiroConfig
|
|||
/**
|
||||
* cookie 属性设置
|
||||
*/
|
||||
public SimpleCookie rememberMeCookie()
|
||||
{
|
||||
public SimpleCookie rememberMeCookie() {
|
||||
SimpleCookie cookie = new SimpleCookie("rememberMe");
|
||||
cookie.setDomain(domain);
|
||||
cookie.setPath(path);
|
||||
|
|
@ -353,8 +335,7 @@ public class ShiroConfig
|
|||
/**
|
||||
* 记住我
|
||||
*/
|
||||
public CookieRememberMeManager rememberMeManager()
|
||||
{
|
||||
public CookieRememberMeManager rememberMeManager() {
|
||||
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
|
||||
cookieRememberMeManager.setCookie(rememberMeCookie());
|
||||
cookieRememberMeManager.setCipherKey(Base64.decode(cipherKey));
|
||||
|
|
@ -364,8 +345,7 @@ public class ShiroConfig
|
|||
/**
|
||||
* 同一个用户多设备登录限制
|
||||
*/
|
||||
public KickoutSessionFilter kickoutSessionFilter()
|
||||
{
|
||||
public KickoutSessionFilter kickoutSessionFilter() {
|
||||
KickoutSessionFilter kickoutSessionFilter = new KickoutSessionFilter();
|
||||
kickoutSessionFilter.setCacheManager(getEhCacheManager());
|
||||
kickoutSessionFilter.setSessionManager(sessionManager());
|
||||
|
|
@ -382,8 +362,7 @@ public class ShiroConfig
|
|||
* thymeleaf模板引擎和shiro框架的整合
|
||||
*/
|
||||
@Bean
|
||||
public ShiroDialect shiroDialect()
|
||||
{
|
||||
public ShiroDialect shiroDialect() {
|
||||
return new ShiroDialect();
|
||||
}
|
||||
|
||||
|
|
@ -391,11 +370,21 @@ public class ShiroConfig
|
|||
* 开启Shiro注解通知器
|
||||
*/
|
||||
@Bean
|
||||
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
|
||||
@Qualifier("securityManager") SecurityManager securityManager)
|
||||
{
|
||||
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor( @Qualifier("securityManager") SecurityManager securityManager) {
|
||||
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
|
||||
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
|
||||
return authorizationAttributeSourceAdvisor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下面的代码是添加注解支持
|
||||
*/
|
||||
@Bean
|
||||
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
|
||||
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
|
||||
// 强制使用cglib,防止重复代理和可能引起代理出错的问题
|
||||
// https://zhuanlan.zhihu.com/p/29161098
|
||||
defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
|
||||
return defaultAdvisorAutoProxyCreator;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue