43 changed files with 1117 additions and 729 deletions
@ -0,0 +1,40 @@ |
|||
package com.zdxt.common.util; |
|||
|
|||
|
|||
import cn.hutool.core.codec.Base64; |
|||
import org.apache.shiro.codec.Hex; |
|||
import org.apache.shiro.crypto.hash.SimpleHash; |
|||
//import org.apache.shiro.lang.codec.Base64;
|
|||
//import org.apache.shiro.lang.codec.Hex;
|
|||
|
|||
public class Md5Hash extends SimpleHash { |
|||
public static final String ALGORITHM_NAME = "MD5"; |
|||
|
|||
public Md5Hash() { |
|||
super("MD5"); |
|||
} |
|||
|
|||
public Md5Hash(Object source) { |
|||
super("MD5", source); |
|||
} |
|||
|
|||
public Md5Hash(Object source, Object salt) { |
|||
super("MD5", source, salt); |
|||
} |
|||
|
|||
public Md5Hash(Object source, Object salt, int hashIterations) { |
|||
super("MD5", source, salt, hashIterations); |
|||
} |
|||
|
|||
public static Md5Hash fromHexString(String hex) { |
|||
Md5Hash hash = new Md5Hash(); |
|||
hash.setBytes(Hex.decode(hex)); |
|||
return hash; |
|||
} |
|||
|
|||
public static Md5Hash fromBase64String(String base64) { |
|||
Md5Hash hash = new Md5Hash(); |
|||
hash.setBytes(Base64.decode(base64)); |
|||
return hash; |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
package com.zdxt.auth.common.utils; |
|||
|
|||
import javax.crypto.KeyGenerator; |
|||
import javax.crypto.SecretKey; |
|||
import java.security.NoSuchAlgorithmException; |
|||
|
|||
/** |
|||
* shiro 密钥生成工具 |
|||
*/ |
|||
public class CipherKeyUtil { |
|||
|
|||
/** |
|||
* 随机生成秘钥,参考org.apache.shiro.crypto.AbstractSymmetricCipherService#generateNewKey(int) |
|||
* @return |
|||
*/ |
|||
public static byte[] generateNewKey() { |
|||
KeyGenerator kg; |
|||
try { |
|||
kg = KeyGenerator.getInstance("AES"); |
|||
} catch (NoSuchAlgorithmException var5) { |
|||
String msg = "Unable to acquire AES algorithm. This is required to function."; |
|||
throw new IllegalStateException(msg, var5); |
|||
} |
|||
kg.init(128); |
|||
SecretKey key = kg.generateKey(); |
|||
byte[] encoded = key.getEncoded(); |
|||
return encoded; |
|||
} |
|||
} |
|||
@ -1,352 +1,353 @@ |
|||
package com.zdxt.auth.framework.config; |
|||
|
|||
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect; |
|||
import com.zdxt.auth.common.utils.StringUtils; |
|||
import com.zdxt.auth.framework.shiro.realm.ShiroCasUserRealm; |
|||
import com.zdxt.auth.framework.shiro.session.OnlineSessionFactory; |
|||
import com.zdxt.auth.framework.shiro.web.filter.CasFilter; |
|||
import com.zdxt.auth.framework.shiro.web.session.OnlineWebSessionManager; |
|||
import com.zdxt.auth.framework.shiro.web.session.SpringSessionValidationScheduler; |
|||
import com.zdxt.common.util.SpringUtils; |
|||
import org.apache.commons.io.IOUtils; |
|||
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy; |
|||
import org.apache.shiro.authc.pam.ModularRealmAuthenticator; |
|||
import org.apache.shiro.cache.MemoryConstrainedCacheManager; |
|||
import org.apache.shiro.cache.ehcache.EhCacheManager; |
|||
import org.apache.shiro.cas.CasSubjectFactory; |
|||
import org.apache.shiro.config.ConfigurationException; |
|||
import org.apache.shiro.io.ResourceUtils; |
|||
import org.apache.shiro.spring.LifecycleBeanPostProcessor; |
|||
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; |
|||
import org.apache.shiro.spring.web.ShiroFilterFactoryBean; |
|||
import org.apache.shiro.web.filter.authc.LogoutFilter; |
|||
import org.apache.shiro.web.mgt.DefaultWebSecurityManager; |
|||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.web.servlet.FilterRegistrationBean; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.context.annotation.Profile; |
|||
import org.springframework.web.filter.DelegatingFilterProxy; |
|||
|
|||
import javax.servlet.Filter; |
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.util.HashMap; |
|||
import java.util.LinkedHashMap; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* Shiro和Cas的整合配置文件 |
|||
* |
|||
* @author: QingWang.Li |
|||
* @date: 2020/5/13 14:49 |
|||
*/ |
|||
// 启用cas环境时生效,环境切换在application.yml中配置
|
|||
@Profile("cas") |
|||
@Configuration |
|||
public class ShiroCasConfiguration { |
|||
|
|||
// Session超时时间,单位为毫秒(默认30分钟)
|
|||
@Value("${shiro.session.expireTime}") |
|||
private int expireTime; |
|||
|
|||
// 设置Cookie的域名
|
|||
@Value("${shiro.cookie.domain}") |
|||
private String domain; |
|||
|
|||
// 设置cookie的有效访问路径
|
|||
@Value("${shiro.cookie.path}") |
|||
private String path; |
|||
|
|||
// 设置HttpOnly属性
|
|||
@Value("${shiro.cookie.httpOnly}") |
|||
private boolean httpOnly; |
|||
|
|||
// 设置Cookie的过期时间,秒为单位
|
|||
@Value("${shiro.cookie.maxAge}") |
|||
private int maxAge; |
|||
|
|||
// 登录地址
|
|||
@Value("${shiro.casLoginUrl}") |
|||
private String loginUrl; |
|||
|
|||
/** |
|||
* 缓存管理器 使用Ehcache实现 |
|||
*/ |
|||
@Bean |
|||
public EhCacheManager getEhCacheManager() { |
|||
net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.getCacheManager("ruoyi"); |
|||
EhCacheManager em = new EhCacheManager(); |
|||
if (StringUtils.isNull(cacheManager)) { |
|||
em.setCacheManager(new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream())); |
|||
return em; |
|||
} else { |
|||
em.setCacheManager(cacheManager); |
|||
return em; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 返回配置文件流 避免ehcache配置文件一直被占用,无法完全销毁项目重新部署 |
|||
*/ |
|||
protected InputStream getCacheManagerConfigFileInputStream() { |
|||
String configFile = "classpath:ehcache/ehcache-shiro.xml"; |
|||
InputStream inputStream = null; |
|||
try { |
|||
inputStream = ResourceUtils.getInputStreamForPath(configFile); |
|||
byte[] b = IOUtils.toByteArray(inputStream); |
|||
InputStream in = new ByteArrayInputStream(b); |
|||
return in; |
|||
} catch (IOException e) { |
|||
throw new ConfigurationException( |
|||
"Unable to obtain input stream for cacheManagerConfigFile [" + configFile + "]", e); |
|||
} finally { |
|||
IOUtils.closeQuietly(inputStream); |
|||
} |
|||
} |
|||
|
|||
@Bean(name = "casRealm") |
|||
public ShiroCasUserRealm casRealm(@Value("${shiro.casServerUrlPrefix}") String casServerUrlPrefix, |
|||
@Value("${shiro.shiroServerUrlPrefix}") String shiroServerUrlPrefix, |
|||
@Value("${shiro.casFilterUrlPattern}") String casFilterUrlPattern, |
|||
EhCacheManager cacheManager) { |
|||
ShiroCasUserRealm casRealm = new ShiroCasUserRealm(); |
|||
// 认证通过后的默认角色
|
|||
casRealm.setDefaultRoles("ROLE_USER"); |
|||
// cas服务端地址前缀
|
|||
casRealm.setCasServerUrlPrefix(casServerUrlPrefix); |
|||
// 应用服务地址,用来接收cas服务端票证
|
|||
casRealm.setCasService(shiroServerUrlPrefix + casFilterUrlPattern); |
|||
|
|||
casRealm.setCacheManager(cacheManager); |
|||
return casRealm; |
|||
} |
|||
|
|||
//package com.zdxt.auth.framework.config;
|
|||
//
|
|||
//import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
|
|||
//import com.zdxt.auth.common.utils.StringUtils;
|
|||
//import com.zdxt.auth.framework.shiro.realm.ShiroCasUserRealm;
|
|||
//import com.zdxt.auth.framework.shiro.session.OnlineSessionFactory;
|
|||
//import com.zdxt.auth.framework.shiro.web.filter.CasFilter;
|
|||
//import com.zdxt.auth.framework.shiro.web.session.OnlineWebSessionManager;
|
|||
//import com.zdxt.auth.framework.shiro.web.session.SpringSessionValidationScheduler;
|
|||
//import com.zdxt.common.util.SpringUtils;
|
|||
//import org.apache.commons.io.IOUtils;
|
|||
//import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
|
|||
//import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
|
|||
//import org.apache.shiro.cache.MemoryConstrainedCacheManager;
|
|||
//import org.apache.shiro.cache.ehcache.EhCacheManager;
|
|||
//import org.apache.shiro.cas.CasSubjectFactory;
|
|||
//import org.apache.shiro.config.ConfigurationException;
|
|||
////import org.apache.shiro.io.ResourceUtils;
|
|||
//import org.apache.shiro.lang.io.ResourceUtils;
|
|||
//import org.apache.shiro.spring.LifecycleBeanPostProcessor;
|
|||
//import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
|
|||
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
|||
//import org.apache.shiro.web.filter.authc.LogoutFilter;
|
|||
//import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
|||
//import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
|||
//import org.springframework.beans.factory.annotation.Value;
|
|||
//import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|||
//import org.springframework.context.annotation.Bean;
|
|||
//import org.springframework.context.annotation.Configuration;
|
|||
//import org.springframework.context.annotation.Profile;
|
|||
//import org.springframework.web.filter.DelegatingFilterProxy;
|
|||
//
|
|||
//import javax.servlet.Filter;
|
|||
//import java.io.ByteArrayInputStream;
|
|||
//import java.io.IOException;
|
|||
//import java.io.InputStream;
|
|||
//import java.util.HashMap;
|
|||
//import java.util.LinkedHashMap;
|
|||
//import java.util.Map;
|
|||
//
|
|||
///**
|
|||
// * Shiro和Cas的整合配置文件
|
|||
// *
|
|||
// * @author: QingWang.Li
|
|||
// * @date: 2020/5/13 14:49
|
|||
// */
|
|||
//// 启用cas环境时生效,环境切换在application.yml中配置
|
|||
//@Profile("cas")
|
|||
//@Configuration
|
|||
//public class ShiroCasConfiguration {
|
|||
//
|
|||
// // Session超时时间,单位为毫秒(默认30分钟)
|
|||
// @Value("${shiro.session.expireTime}")
|
|||
// private int expireTime;
|
|||
//
|
|||
// // 设置Cookie的域名
|
|||
// @Value("${shiro.cookie.domain}")
|
|||
// private String domain;
|
|||
//
|
|||
// // 设置cookie的有效访问路径
|
|||
// @Value("${shiro.cookie.path}")
|
|||
// private String path;
|
|||
//
|
|||
// // 设置HttpOnly属性
|
|||
// @Value("${shiro.cookie.httpOnly}")
|
|||
// private boolean httpOnly;
|
|||
//
|
|||
// // 设置Cookie的过期时间,秒为单位
|
|||
// @Value("${shiro.cookie.maxAge}")
|
|||
// private int maxAge;
|
|||
//
|
|||
// // 登录地址
|
|||
// @Value("${shiro.casLoginUrl}")
|
|||
// private String loginUrl;
|
|||
//
|
|||
// /**
|
|||
// * 自定义sessionDAO会话
|
|||
// * 缓存管理器 使用Ehcache实现
|
|||
// */
|
|||
// @Bean
|
|||
// public OnlineSessionDAO sessionDAO() {
|
|||
// OnlineSessionDAO sessionDAO = new OnlineSessionDAO();
|
|||
// return sessionDAO;
|
|||
// public EhCacheManager getEhCacheManager() {
|
|||
// net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.getCacheManager("ruoyi");
|
|||
// EhCacheManager em = new EhCacheManager();
|
|||
// if (StringUtils.isNull(cacheManager)) {
|
|||
// em.setCacheManager(new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream()));
|
|||
// return em;
|
|||
// } else {
|
|||
// em.setCacheManager(cacheManager);
|
|||
// return em;
|
|||
// }
|
|||
// }
|
|||
|
|||
/** |
|||
* 自定义sessionFactory会话 |
|||
*/ |
|||
@Bean |
|||
public OnlineSessionFactory sessionFactory() { |
|||
OnlineSessionFactory sessionFactory = new OnlineSessionFactory(); |
|||
return sessionFactory; |
|||
} |
|||
|
|||
//
|
|||
// /**
|
|||
// * 会话管理器
|
|||
// * 返回配置文件流 避免ehcache配置文件一直被占用,无法完全销毁项目重新部署
|
|||
// */
|
|||
// protected InputStream getCacheManagerConfigFileInputStream() {
|
|||
// String configFile = "classpath:ehcache/ehcache-shiro.xml";
|
|||
// InputStream inputStream = null;
|
|||
// try {
|
|||
// inputStream = ResourceUtils.getInputStreamForPath(configFile);
|
|||
// byte[] b = IOUtils.toByteArray(inputStream);
|
|||
// InputStream in = new ByteArrayInputStream(b);
|
|||
// return in;
|
|||
// } catch (IOException e) {
|
|||
// throw new ConfigurationException(
|
|||
// "Unable to obtain input stream for cacheManagerConfigFile [" + configFile + "]", e);
|
|||
// } finally {
|
|||
// IOUtils.closeQuietly(inputStream);
|
|||
// }
|
|||
// }
|
|||
//
|
|||
// @Bean(name = "casRealm")
|
|||
// public ShiroCasUserRealm casRealm(@Value("${shiro.casServerUrlPrefix}") String casServerUrlPrefix,
|
|||
// @Value("${shiro.shiroServerUrlPrefix}") String shiroServerUrlPrefix,
|
|||
// @Value("${shiro.casFilterUrlPattern}") String casFilterUrlPattern,
|
|||
// EhCacheManager cacheManager) {
|
|||
// ShiroCasUserRealm casRealm = new ShiroCasUserRealm();
|
|||
// // 认证通过后的默认角色
|
|||
// casRealm.setDefaultRoles("ROLE_USER");
|
|||
// // cas服务端地址前缀
|
|||
// casRealm.setCasServerUrlPrefix(casServerUrlPrefix);
|
|||
// // 应用服务地址,用来接收cas服务端票证
|
|||
// casRealm.setCasService(shiroServerUrlPrefix + casFilterUrlPattern);
|
|||
//
|
|||
// casRealm.setCacheManager(cacheManager);
|
|||
// return casRealm;
|
|||
// }
|
|||
//
|
|||
//// /**
|
|||
//// * 自定义sessionDAO会话
|
|||
//// */
|
|||
//// @Bean
|
|||
//// public OnlineSessionDAO sessionDAO() {
|
|||
//// OnlineSessionDAO sessionDAO = new OnlineSessionDAO();
|
|||
//// return sessionDAO;
|
|||
//// }
|
|||
//
|
|||
// /**
|
|||
// * 自定义sessionFactory会话
|
|||
// */
|
|||
// @Bean
|
|||
// public OnlineSessionFactory sessionFactory() {
|
|||
// OnlineSessionFactory sessionFactory = new OnlineSessionFactory();
|
|||
// return sessionFactory;
|
|||
// }
|
|||
//
|
|||
//// /**
|
|||
//// * 会话管理器
|
|||
//// */
|
|||
//// @Bean
|
|||
//// public OnlineWebSessionManager sessionManager() {
|
|||
//// OnlineWebSessionManager manager = new OnlineWebSessionManager();
|
|||
//// // 加入缓存管理器
|
|||
//// manager.setCacheManager(getEhCacheManager());
|
|||
//// // 删除过期的session
|
|||
//// manager.setDeleteInvalidSessions(true);
|
|||
//// // 设置全局session超时时间
|
|||
//// manager.setGlobalSessionTimeout(expireTime * 60 * 1000);
|
|||
//// // 去掉 JSESSIONID
|
|||
//// manager.setSessionIdUrlRewritingEnabled(false);
|
|||
//// // 定义要使用的无效的Session定时调度器
|
|||
//// manager.setSessionValidationScheduler(SpringUtils.getBean(SpringSessionValidationScheduler.class));
|
|||
//// // 是否定时检查session
|
|||
//// manager.setSessionValidationSchedulerEnabled(true);
|
|||
//// // 自定义SessionDao
|
|||
//// manager.setSessionDAO(sessionDAO());
|
|||
//// // 自定义sessionFactory
|
|||
//// manager.setSessionFactory(sessionFactory());
|
|||
//// return manager;
|
|||
//// }
|
|||
//
|
|||
// /**
|
|||
// * 配置安全管理器
|
|||
// **/
|
|||
// @Bean(name = "securityManager")
|
|||
// public DefaultWebSecurityManager defaultWebSecurityManager(ModularRealmAuthenticator authenticator,
|
|||
// ShiroCasUserRealm casRealm) {
|
|||
// DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
|||
// // 设置授权策略,此步骤必须在设置realm的前面,不然会报错realm未配置
|
|||
// securityManager.setAuthenticator(authenticator);
|
|||
// securityManager.setSubjectFactory(new CasSubjectFactory());
|
|||
//
|
|||
// // 缓存管理器
|
|||
// securityManager.setCacheManager(new MemoryConstrainedCacheManager());
|
|||
// // 设置自定义验证策略
|
|||
// securityManager.setRealm(casRealm);
|
|||
//
|
|||
// return securityManager;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 退出过滤器
|
|||
// */
|
|||
// public com.zdxt.auth.framework.shiro.web.filter.LogoutFilter logoutFilter() {
|
|||
// com.zdxt.auth.framework.shiro.web.filter.LogoutFilter logoutFilter = new com.zdxt.auth.framework.shiro.web.filter.LogoutFilter();
|
|||
// logoutFilter.setCacheManager(getEhCacheManager());
|
|||
// logoutFilter.setLoginUrl(loginUrl);
|
|||
// return logoutFilter;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 配置登录过滤器
|
|||
// */
|
|||
// @Bean(name = "casFilter")
|
|||
// public CasFilter casFilter(@Value("${shiro.casLoginUrl}") String loginUrl) {
|
|||
// CasFilter casFilter = new CasFilter();
|
|||
// casFilter.setName("casFilter");
|
|||
// casFilter.setEnabled(true);
|
|||
// casFilter.setFailureUrl(loginUrl);
|
|||
// return casFilter;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * shiro 过滤器
|
|||
// */
|
|||
// @Bean(name = "shiroFilter")
|
|||
// public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager,
|
|||
// CasFilter casFilter,
|
|||
// @Value("${shiro.casLogoutUrl}") String logoutUrl,
|
|||
// @Value("${shiro.casLoginUrl}") String loginUrl,
|
|||
// @Value("${shiro.casFilterUrlPattern}") String casFilterUrlPattern) {
|
|||
// ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|||
// // 设置安全管理器
|
|||
// shiroFilterFactoryBean.setSecurityManager(securityManager);
|
|||
// // 设置登录地址
|
|||
// shiroFilterFactoryBean.setLoginUrl(loginUrl);
|
|||
// // 设置登录成功地址
|
|||
// shiroFilterFactoryBean.setSuccessUrl("/");
|
|||
// // 配置拦截地址
|
|||
// Map<String, Filter> filters = new HashMap<>();
|
|||
// filters.put("casFilter", casFilter);
|
|||
//
|
|||
// // 配置登出地址 5.0版本和3.0版本
|
|||
// LogoutFilter logoutFilter = new LogoutFilter();
|
|||
// logoutFilter.setRedirectUrl(logoutUrl);
|
|||
// filters.put("logout", logoutFilter);
|
|||
//
|
|||
// // 注销成功,则跳转到指定页面 4.0版本
|
|||
// // filters.put("logout", logoutFilter());
|
|||
//
|
|||
// shiroFilterFactoryBean.setFilters(filters);
|
|||
// // 设置访问用户页面需要授权的操作
|
|||
// loadShiroFilterChain(shiroFilterFactoryBean, casFilterUrlPattern);
|
|||
// // 将设置的权限设置到shiroFilterFactoryBean
|
|||
// return shiroFilterFactoryBean;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 1、当我们第一次访问客户端时,先去cas进行认证,成功后会返回一个ticket
|
|||
// * 2、返回的ticket地址在casRealm已经进行了配置,shiroServerUrlPrefix + casFilterUrlPattern
|
|||
// * 3、即地址为/shiro-cas,对该地址进行casFilter拦截
|
|||
// */
|
|||
// private void loadShiroFilterChain(ShiroFilterFactoryBean shiroFilterFactoryBean, String casFilterUrlPattern) {
|
|||
// Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
|
|||
//
|
|||
// filterChainDefinitionMap.put(casFilterUrlPattern, "casFilter");
|
|||
//
|
|||
// // 对静态资源设置匿名访问
|
|||
// filterChainDefinitionMap.put("/favicon.ico**", "anon");
|
|||
// filterChainDefinitionMap.put("/ruoyi.png**", "anon");
|
|||
// filterChainDefinitionMap.put("/css/**", "anon");
|
|||
// filterChainDefinitionMap.put("/docs/**", "anon");
|
|||
// filterChainDefinitionMap.put("/fonts/**", "anon");
|
|||
// filterChainDefinitionMap.put("/img/**", "anon");
|
|||
// filterChainDefinitionMap.put("/ajax/**", "anon");
|
|||
// filterChainDefinitionMap.put("/js/**", "anon");
|
|||
// filterChainDefinitionMap.put("/ruoyi/**", "anon");
|
|||
// // 附件访问不拦截
|
|||
// filterChainDefinitionMap.put("/profile/**", "anon");
|
|||
// // 不拦截app登录
|
|||
// filterChainDefinitionMap.put("/appAjaxLogin", "anon");
|
|||
// filterChainDefinitionMap.put("/app/appLogin", "anon");
|
|||
// filterChainDefinitionMap.put("/app/**", "anon");
|
|||
// // 金蝶同步数据不拦截
|
|||
// filterChainDefinitionMap.put("/kingdee/dataSync/**", "anon");
|
|||
// filterChainDefinitionMap.put("/captcha/captchaImage**", "anon");
|
|||
//
|
|||
// // 对外开放的controller不拦截
|
|||
// filterChainDefinitionMap.put("/external/**", "anon");
|
|||
//
|
|||
// // 退出 logout地址,shiro去清除session
|
|||
// filterChainDefinitionMap.put("/logout", "logout");
|
|||
// filterChainDefinitionMap.put("/**", "authc");
|
|||
//
|
|||
// shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 添加shiro的filter
|
|||
// */
|
|||
// @Bean
|
|||
// public FilterRegistrationBean filterRegistrationBean() {
|
|||
// FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
|
|||
// filterRegistration.setFilter(new DelegatingFilterProxy("shiroFilter"));
|
|||
// filterRegistration.addInitParameter("targetFilterLifecycle", "true");
|
|||
// filterRegistration.setEnabled(true);
|
|||
// filterRegistration.addUrlPatterns("/*");
|
|||
// return filterRegistration;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 保证了shiro内部lifecycle函数bean的执行
|
|||
// */
|
|||
// @Bean(name = "lifecycleBeanPostProcessor")
|
|||
// public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
|
|||
// return new LifecycleBeanPostProcessor();
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 配置授权策略
|
|||
// */
|
|||
// @Bean(name = "authenticator")
|
|||
// public ModularRealmAuthenticator modularRealmAuthenticator() {
|
|||
// ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
|
|||
// authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
|
|||
// return authenticator;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 开启Shiro的注解(如@RequiresPermissions)
|
|||
// * 需借助SpringAOP扫描使用Shiro注解的类
|
|||
// * 配置以下两个bean(DefaultAdvisorAutoProxyCreator和AuthorizationAttributeSourceAdvisor)即可实现此功能
|
|||
// *
|
|||
// * @return
|
|||
// */
|
|||
// @Bean
|
|||
// public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
|
|||
// DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
|
|||
// advisorAutoProxyCreator.setProxyTargetClass(true);
|
|||
// return advisorAutoProxyCreator;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 开启aop注解支持
|
|||
// *
|
|||
// * @param securityManager
|
|||
// * @return
|
|||
// */
|
|||
// @Bean
|
|||
// public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
|
|||
// AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
|
|||
// authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
|
|||
// return authorizationAttributeSourceAdvisor;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * thymeleaf模板引擎和shiro框架的整合
|
|||
// */
|
|||
// @Bean
|
|||
// public OnlineWebSessionManager sessionManager() {
|
|||
// OnlineWebSessionManager manager = new OnlineWebSessionManager();
|
|||
// // 加入缓存管理器
|
|||
// manager.setCacheManager(getEhCacheManager());
|
|||
// // 删除过期的session
|
|||
// manager.setDeleteInvalidSessions(true);
|
|||
// // 设置全局session超时时间
|
|||
// manager.setGlobalSessionTimeout(expireTime * 60 * 1000);
|
|||
// // 去掉 JSESSIONID
|
|||
// manager.setSessionIdUrlRewritingEnabled(false);
|
|||
// // 定义要使用的无效的Session定时调度器
|
|||
// manager.setSessionValidationScheduler(SpringUtils.getBean(SpringSessionValidationScheduler.class));
|
|||
// // 是否定时检查session
|
|||
// manager.setSessionValidationSchedulerEnabled(true);
|
|||
// // 自定义SessionDao
|
|||
// manager.setSessionDAO(sessionDAO());
|
|||
// // 自定义sessionFactory
|
|||
// manager.setSessionFactory(sessionFactory());
|
|||
// return manager;
|
|||
// public ShiroDialect shiroDialect() {
|
|||
// return new ShiroDialect();
|
|||
// }
|
|||
|
|||
/** |
|||
* 配置安全管理器 |
|||
**/ |
|||
@Bean(name = "securityManager") |
|||
public DefaultWebSecurityManager defaultWebSecurityManager(ModularRealmAuthenticator authenticator, |
|||
ShiroCasUserRealm casRealm) { |
|||
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); |
|||
// 设置授权策略,此步骤必须在设置realm的前面,不然会报错realm未配置
|
|||
securityManager.setAuthenticator(authenticator); |
|||
securityManager.setSubjectFactory(new CasSubjectFactory()); |
|||
|
|||
// 缓存管理器
|
|||
securityManager.setCacheManager(new MemoryConstrainedCacheManager()); |
|||
// 设置自定义验证策略
|
|||
securityManager.setRealm(casRealm); |
|||
|
|||
return securityManager; |
|||
} |
|||
|
|||
/** |
|||
* 退出过滤器 |
|||
*/ |
|||
public com.zdxt.auth.framework.shiro.web.filter.LogoutFilter logoutFilter() { |
|||
com.zdxt.auth.framework.shiro.web.filter.LogoutFilter logoutFilter = new com.zdxt.auth.framework.shiro.web.filter.LogoutFilter(); |
|||
logoutFilter.setCacheManager(getEhCacheManager()); |
|||
logoutFilter.setLoginUrl(loginUrl); |
|||
return logoutFilter; |
|||
} |
|||
|
|||
/** |
|||
* 配置登录过滤器 |
|||
*/ |
|||
@Bean(name = "casFilter") |
|||
public CasFilter casFilter(@Value("${shiro.casLoginUrl}") String loginUrl) { |
|||
CasFilter casFilter = new CasFilter(); |
|||
casFilter.setName("casFilter"); |
|||
casFilter.setEnabled(true); |
|||
casFilter.setFailureUrl(loginUrl); |
|||
return casFilter; |
|||
} |
|||
|
|||
/** |
|||
* shiro 过滤器 |
|||
*/ |
|||
@Bean(name = "shiroFilter") |
|||
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager, |
|||
CasFilter casFilter, |
|||
@Value("${shiro.casLogoutUrl}") String logoutUrl, |
|||
@Value("${shiro.casLoginUrl}") String loginUrl, |
|||
@Value("${shiro.casFilterUrlPattern}") String casFilterUrlPattern) { |
|||
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); |
|||
// 设置安全管理器
|
|||
shiroFilterFactoryBean.setSecurityManager(securityManager); |
|||
// 设置登录地址
|
|||
shiroFilterFactoryBean.setLoginUrl(loginUrl); |
|||
// 设置登录成功地址
|
|||
shiroFilterFactoryBean.setSuccessUrl("/"); |
|||
// 配置拦截地址
|
|||
Map<String, Filter> filters = new HashMap<>(); |
|||
filters.put("casFilter", casFilter); |
|||
|
|||
// 配置登出地址 5.0版本和3.0版本
|
|||
LogoutFilter logoutFilter = new LogoutFilter(); |
|||
logoutFilter.setRedirectUrl(logoutUrl); |
|||
filters.put("logout", logoutFilter); |
|||
|
|||
// 注销成功,则跳转到指定页面 4.0版本
|
|||
// filters.put("logout", logoutFilter());
|
|||
|
|||
shiroFilterFactoryBean.setFilters(filters); |
|||
// 设置访问用户页面需要授权的操作
|
|||
loadShiroFilterChain(shiroFilterFactoryBean, casFilterUrlPattern); |
|||
// 将设置的权限设置到shiroFilterFactoryBean
|
|||
return shiroFilterFactoryBean; |
|||
} |
|||
|
|||
/** |
|||
* 1、当我们第一次访问客户端时,先去cas进行认证,成功后会返回一个ticket |
|||
* 2、返回的ticket地址在casRealm已经进行了配置,shiroServerUrlPrefix + casFilterUrlPattern |
|||
* 3、即地址为/shiro-cas,对该地址进行casFilter拦截 |
|||
*/ |
|||
private void loadShiroFilterChain(ShiroFilterFactoryBean shiroFilterFactoryBean, String casFilterUrlPattern) { |
|||
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); |
|||
|
|||
filterChainDefinitionMap.put(casFilterUrlPattern, "casFilter"); |
|||
|
|||
// 对静态资源设置匿名访问
|
|||
filterChainDefinitionMap.put("/favicon.ico**", "anon"); |
|||
filterChainDefinitionMap.put("/ruoyi.png**", "anon"); |
|||
filterChainDefinitionMap.put("/css/**", "anon"); |
|||
filterChainDefinitionMap.put("/docs/**", "anon"); |
|||
filterChainDefinitionMap.put("/fonts/**", "anon"); |
|||
filterChainDefinitionMap.put("/img/**", "anon"); |
|||
filterChainDefinitionMap.put("/ajax/**", "anon"); |
|||
filterChainDefinitionMap.put("/js/**", "anon"); |
|||
filterChainDefinitionMap.put("/ruoyi/**", "anon"); |
|||
// 附件访问不拦截
|
|||
filterChainDefinitionMap.put("/profile/**", "anon"); |
|||
// 不拦截app登录
|
|||
filterChainDefinitionMap.put("/appAjaxLogin", "anon"); |
|||
filterChainDefinitionMap.put("/app/appLogin", "anon"); |
|||
filterChainDefinitionMap.put("/app/**", "anon"); |
|||
// 金蝶同步数据不拦截
|
|||
filterChainDefinitionMap.put("/kingdee/dataSync/**", "anon"); |
|||
filterChainDefinitionMap.put("/captcha/captchaImage**", "anon"); |
|||
|
|||
// 对外开放的controller不拦截
|
|||
filterChainDefinitionMap.put("/external/**", "anon"); |
|||
|
|||
// 退出 logout地址,shiro去清除session
|
|||
filterChainDefinitionMap.put("/logout", "logout"); |
|||
filterChainDefinitionMap.put("/**", "authc"); |
|||
|
|||
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); |
|||
} |
|||
|
|||
/** |
|||
* 添加shiro的filter |
|||
*/ |
|||
@Bean |
|||
public FilterRegistrationBean filterRegistrationBean() { |
|||
FilterRegistrationBean filterRegistration = new FilterRegistrationBean(); |
|||
filterRegistration.setFilter(new DelegatingFilterProxy("shiroFilter")); |
|||
filterRegistration.addInitParameter("targetFilterLifecycle", "true"); |
|||
filterRegistration.setEnabled(true); |
|||
filterRegistration.addUrlPatterns("/*"); |
|||
return filterRegistration; |
|||
} |
|||
|
|||
/** |
|||
* 保证了shiro内部lifecycle函数bean的执行 |
|||
*/ |
|||
@Bean(name = "lifecycleBeanPostProcessor") |
|||
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { |
|||
return new LifecycleBeanPostProcessor(); |
|||
} |
|||
|
|||
/** |
|||
* 配置授权策略 |
|||
*/ |
|||
@Bean(name = "authenticator") |
|||
public ModularRealmAuthenticator modularRealmAuthenticator() { |
|||
ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator(); |
|||
authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy()); |
|||
return authenticator; |
|||
} |
|||
|
|||
/** |
|||
* 开启Shiro的注解(如@RequiresPermissions) |
|||
* 需借助SpringAOP扫描使用Shiro注解的类 |
|||
* 配置以下两个bean(DefaultAdvisorAutoProxyCreator和AuthorizationAttributeSourceAdvisor)即可实现此功能 |
|||
* |
|||
* @return |
|||
*/ |
|||
@Bean |
|||
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() { |
|||
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); |
|||
advisorAutoProxyCreator.setProxyTargetClass(true); |
|||
return advisorAutoProxyCreator; |
|||
} |
|||
|
|||
/** |
|||
* 开启aop注解支持 |
|||
* |
|||
* @param securityManager |
|||
* @return |
|||
*/ |
|||
@Bean |
|||
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) { |
|||
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); |
|||
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); |
|||
return authorizationAttributeSourceAdvisor; |
|||
} |
|||
|
|||
/** |
|||
* thymeleaf模板引擎和shiro框架的整合 |
|||
*/ |
|||
@Bean |
|||
public ShiroDialect shiroDialect() { |
|||
return new ShiroDialect(); |
|||
} |
|||
|
|||
} |
|||
//
|
|||
//}
|
|||
@ -1,162 +1,162 @@ |
|||
package com.zdxt.auth.framework.shiro.realm; |
|||
|
|||
import cn.hutool.core.bean.BeanUtil; |
|||
import com.zdxt.auth.common.exception.user.*; |
|||
import com.zdxt.auth.common.utils.security.ShiroUtils; |
|||
import com.zdxt.auth.framework.shiro.service.LoginService; |
|||
import com.zdxt.auth.project.system.menu.service.IMenuService; |
|||
import com.zdxt.auth.project.system.role.service.IRoleService; |
|||
import com.zdxt.auth.project.system.user.mapper.UserMapper; |
|||
import com.zdxt.domain.auth.User; |
|||
import org.apache.shiro.SecurityUtils; |
|||
import org.apache.shiro.authc.*; |
|||
import org.apache.shiro.authz.AuthorizationInfo; |
|||
import org.apache.shiro.authz.SimpleAuthorizationInfo; |
|||
import org.apache.shiro.cas.CasRealm; |
|||
import org.apache.shiro.cas.CasToken; |
|||
import org.apache.shiro.subject.PrincipalCollection; |
|||
import org.apache.shiro.subject.SimplePrincipalCollection; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
|
|||
import java.util.HashSet; |
|||
import java.util.Set; |
|||
|
|||
/** |
|||
* shiro和cas整合的Realm 处理登录 权限 |
|||
* @author: QingWang.Li |
|||
* @date: 2020/5/13 14:17 |
|||
*/ |
|||
public class ShiroCasUserRealm extends CasRealm implements ZdxtUserRealm { |
|||
private static Logger log = LoggerFactory.getLogger(ShiroCasUserRealm.class); |
|||
|
|||
@Autowired |
|||
private IMenuService menuService; |
|||
|
|||
@Autowired |
|||
private IRoleService roleService; |
|||
|
|||
@Autowired |
|||
private LoginService loginService; |
|||
|
|||
@Autowired |
|||
private UserMapper userMapper; |
|||
|
|||
/** |
|||
* 在调用subject.login()时,首先调用此接口 |
|||
*/ |
|||
@Override |
|||
public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { |
|||
// 调用父类的方法,然后授权用户
|
|||
AuthenticationInfo authc = super.doGetAuthenticationInfo(token); |
|||
// 获得用户名
|
|||
String username = (String) authc.getPrincipals().getPrimaryPrincipal(); |
|||
|
|||
// 获取ticket
|
|||
CasToken casToken = (CasToken) token; |
|||
String ticket = (String) casToken.getCredentials(); |
|||
if(ticket!=null&&!ticket.equals("")){ |
|||
// 从数据库中获取用户信息
|
|||
User user = userMapper.selectUserByLoginName(username); |
|||
if (BeanUtil.isNotEmpty(user)) { |
|||
PrincipalCollection principalCollection = new SimplePrincipalCollection(user, getName()); |
|||
|
|||
// 返回携带用户信息和ticket的信息对象
|
|||
return new SimpleAuthenticationInfo(principalCollection, ticket); |
|||
} |
|||
return null; |
|||
}else{ |
|||
UsernamePasswordToken upToken = (UsernamePasswordToken) token; |
|||
String username1 = upToken.getUsername(); |
|||
String password = ""; |
|||
if (upToken.getPassword() != null) |
|||
{ |
|||
password = new String(upToken.getPassword()); |
|||
} |
|||
|
|||
User user = null; |
|||
try |
|||
{ |
|||
user = loginService.login(username1, password); |
|||
} |
|||
catch (CaptchaException e) |
|||
{ |
|||
throw new AuthenticationException(e.getMessage(), e); |
|||
} |
|||
catch (UserNotExistsException e) |
|||
{ |
|||
throw new UnknownAccountException(e.getMessage(), e); |
|||
} |
|||
catch (UserPasswordNotMatchException e) |
|||
{ |
|||
throw new IncorrectCredentialsException(e.getMessage(), e); |
|||
} |
|||
catch (UserPasswordRetryLimitExceedException e) |
|||
{ |
|||
throw new ExcessiveAttemptsException(e.getMessage(), e); |
|||
} |
|||
catch (UserBlockedException e) |
|||
{ |
|||
throw new LockedAccountException(e.getMessage(), e); |
|||
} |
|||
catch (RoleBlockedException e) |
|||
{ |
|||
throw new LockedAccountException(e.getMessage(), e); |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage()); |
|||
throw new AuthenticationException(e.getMessage(), e); |
|||
} |
|||
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName()); |
|||
return info; |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 进行权限验证的时候,调用方法,将用户的权限信息写进SimpleAuthorizationInfo |
|||
*/ |
|||
@Override |
|||
public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { |
|||
// 用户名称
|
|||
log.info("进入了权限认证"); |
|||
Object username = principals.getPrimaryPrincipal(); |
|||
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); |
|||
// 从数据库获取用户权限
|
|||
|
|||
// 简单例子
|
|||
// Set<String> permission = new HashSet<>();
|
|||
// permission.add("sys:dept:list");
|
|||
// info.setStringPermissions(permission);
|
|||
|
|||
User user = ShiroUtils.getSysUser(); |
|||
// 角色列表
|
|||
Set<String> roles = new HashSet<String>(); |
|||
// 功能列表
|
|||
Set<String> menus = new HashSet<String>(); |
|||
// 管理员拥有所有权限
|
|||
if (user.isAdmin()) { |
|||
info.addRole("admin"); |
|||
info.addStringPermission("*:*:*"); |
|||
} else { |
|||
roles = roleService.selectRoleKeys(user.getUserId()); |
|||
menus = menuService.selectPermsByUserId(user.getUserId()); |
|||
// 角色加入AuthorizationInfo认证对象
|
|||
info.setRoles(roles); |
|||
// 权限加入AuthorizationInfo认证对象
|
|||
info.setStringPermissions(menus); |
|||
} |
|||
return info; |
|||
} |
|||
|
|||
/** |
|||
* 清理缓存权限 |
|||
*/ |
|||
@Override |
|||
public void clearCachedAuthorizationInfo() |
|||
{ |
|||
this.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals()); |
|||
} |
|||
} |
|||
//package com.zdxt.auth.framework.shiro.realm;
|
|||
//
|
|||
//import cn.hutool.core.bean.BeanUtil;
|
|||
//import com.zdxt.auth.common.exception.user.*;
|
|||
//import com.zdxt.auth.common.utils.security.ShiroUtils;
|
|||
//import com.zdxt.auth.framework.shiro.service.LoginService;
|
|||
//import com.zdxt.auth.project.system.menu.service.IMenuService;
|
|||
//import com.zdxt.auth.project.system.role.service.IRoleService;
|
|||
//import com.zdxt.auth.project.system.user.mapper.UserMapper;
|
|||
//import com.zdxt.domain.auth.User;
|
|||
//import org.apache.shiro.SecurityUtils;
|
|||
//import org.apache.shiro.authc.*;
|
|||
//import org.apache.shiro.authz.AuthorizationInfo;
|
|||
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
|
|||
//import org.apache.shiro.cas.CasRealm;
|
|||
//import org.apache.shiro.cas.CasToken;
|
|||
//import org.apache.shiro.subject.PrincipalCollection;
|
|||
//import org.apache.shiro.subject.SimplePrincipalCollection;
|
|||
//import org.slf4j.Logger;
|
|||
//import org.slf4j.LoggerFactory;
|
|||
//import org.springframework.beans.factory.annotation.Autowired;
|
|||
//
|
|||
//import java.util.HashSet;
|
|||
//import java.util.Set;
|
|||
//
|
|||
///**
|
|||
// * shiro和cas整合的Realm 处理登录 权限
|
|||
// * @author: QingWang.Li
|
|||
// * @date: 2020/5/13 14:17
|
|||
// */
|
|||
//public class ShiroCasUserRealm extends CasRealm implements ZdxtUserRealm {
|
|||
// private static Logger log = LoggerFactory.getLogger(ShiroCasUserRealm.class);
|
|||
//
|
|||
// @Autowired
|
|||
// private IMenuService menuService;
|
|||
//
|
|||
// @Autowired
|
|||
// private IRoleService roleService;
|
|||
//
|
|||
// @Autowired
|
|||
// private LoginService loginService;
|
|||
//
|
|||
// @Autowired
|
|||
// private UserMapper userMapper;
|
|||
//
|
|||
// /**
|
|||
// * 在调用subject.login()时,首先调用此接口
|
|||
// */
|
|||
// @Override
|
|||
// public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
|
|||
// // 调用父类的方法,然后授权用户
|
|||
// AuthenticationInfo authc = super.doGetAuthenticationInfo(token);
|
|||
// // 获得用户名
|
|||
// String username = (String) authc.getPrincipals().getPrimaryPrincipal();
|
|||
//
|
|||
// // 获取ticket
|
|||
// CasToken casToken = (CasToken) token;
|
|||
// String ticket = (String) casToken.getCredentials();
|
|||
// if(ticket!=null&&!ticket.equals("")){
|
|||
// // 从数据库中获取用户信息
|
|||
// User user = userMapper.selectUserByLoginName(username);
|
|||
// if (BeanUtil.isNotEmpty(user)) {
|
|||
// PrincipalCollection principalCollection = new SimplePrincipalCollection(user, getName());
|
|||
//
|
|||
// // 返回携带用户信息和ticket的信息对象
|
|||
// return new SimpleAuthenticationInfo(principalCollection, ticket);
|
|||
// }
|
|||
// return null;
|
|||
// }else{
|
|||
// UsernamePasswordToken upToken = (UsernamePasswordToken) token;
|
|||
// String username1 = upToken.getUsername();
|
|||
// String password = "";
|
|||
// if (upToken.getPassword() != null)
|
|||
// {
|
|||
// password = new String(upToken.getPassword());
|
|||
// }
|
|||
//
|
|||
// User user = null;
|
|||
// try
|
|||
// {
|
|||
// user = loginService.login(username1, password);
|
|||
// }
|
|||
// catch (CaptchaException e)
|
|||
// {
|
|||
// throw new AuthenticationException(e.getMessage(), e);
|
|||
// }
|
|||
// catch (UserNotExistsException e)
|
|||
// {
|
|||
// throw new UnknownAccountException(e.getMessage(), e);
|
|||
// }
|
|||
// catch (UserPasswordNotMatchException e)
|
|||
// {
|
|||
// throw new IncorrectCredentialsException(e.getMessage(), e);
|
|||
// }
|
|||
// catch (UserPasswordRetryLimitExceedException e)
|
|||
// {
|
|||
// throw new ExcessiveAttemptsException(e.getMessage(), e);
|
|||
// }
|
|||
// catch (UserBlockedException e)
|
|||
// {
|
|||
// throw new LockedAccountException(e.getMessage(), e);
|
|||
// }
|
|||
// catch (RoleBlockedException e)
|
|||
// {
|
|||
// throw new LockedAccountException(e.getMessage(), e);
|
|||
// }
|
|||
// catch (Exception e)
|
|||
// {
|
|||
// log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
|
|||
// throw new AuthenticationException(e.getMessage(), e);
|
|||
// }
|
|||
// SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
|
|||
// return info;
|
|||
// }
|
|||
//
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 进行权限验证的时候,调用方法,将用户的权限信息写进SimpleAuthorizationInfo
|
|||
// */
|
|||
// @Override
|
|||
// public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
|
|||
// // 用户名称
|
|||
// log.info("进入了权限认证");
|
|||
// Object username = principals.getPrimaryPrincipal();
|
|||
// SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
|
|||
// // 从数据库获取用户权限
|
|||
//
|
|||
// // 简单例子
|
|||
// // Set<String> permission = new HashSet<>();
|
|||
// // permission.add("sys:dept:list");
|
|||
// // info.setStringPermissions(permission);
|
|||
//
|
|||
// User user = ShiroUtils.getSysUser();
|
|||
// // 角色列表
|
|||
// Set<String> roles = new HashSet<String>();
|
|||
// // 功能列表
|
|||
// Set<String> menus = new HashSet<String>();
|
|||
// // 管理员拥有所有权限
|
|||
// if (user.isAdmin()) {
|
|||
// info.addRole("admin");
|
|||
// info.addStringPermission("*:*:*");
|
|||
// } else {
|
|||
// roles = roleService.selectRoleKeys(user.getUserId());
|
|||
// menus = menuService.selectPermsByUserId(user.getUserId());
|
|||
// // 角色加入AuthorizationInfo认证对象
|
|||
// info.setRoles(roles);
|
|||
// // 权限加入AuthorizationInfo认证对象
|
|||
// info.setStringPermissions(menus);
|
|||
// }
|
|||
// return info;
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 清理缓存权限
|
|||
// */
|
|||
// @Override
|
|||
// public void clearCachedAuthorizationInfo()
|
|||
// {
|
|||
// this.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
|
|||
// }
|
|||
//}
|
|||
@ -1,73 +1,73 @@ |
|||
package com.zdxt.auth.framework.shiro.web.filter; |
|||
|
|||
import org.apache.shiro.authc.AuthenticationToken; |
|||
import org.apache.shiro.cas.CasToken; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.util.StringUtils; |
|||
import org.springframework.web.bind.annotation.RequestMethod; |
|||
|
|||
import javax.servlet.ServletRequest; |
|||
import javax.servlet.ServletResponse; |
|||
import javax.servlet.http.HttpServletRequest; |
|||
|
|||
/** |
|||
* cas单点登录过滤器 |
|||
* |
|||
* @author: QingWang.Li |
|||
* @date: 2020/5/14 9:13 |
|||
*/ |
|||
public class CasFilter extends org.apache.shiro.cas.CasFilter { |
|||
private static Logger logger = LoggerFactory.getLogger(CasFilter.class); |
|||
private static final String TICKET_PARAMETER = "ticket"; |
|||
|
|||
public CasFilter() { |
|||
} |
|||
|
|||
@Override |
|||
public AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception { |
|||
// 获取请求的ticket
|
|||
HttpServletRequest httpRequest = (HttpServletRequest) request; |
|||
String ticket = getRequestTicket(httpRequest); |
|||
if (StringUtils.isEmpty(ticket)) { |
|||
logger.debug("票证获取失败,票证为空!"); |
|||
return null; |
|||
} |
|||
return new CasToken(ticket); |
|||
} |
|||
|
|||
/** |
|||
* 拒绝除了option以外的所有请求 |
|||
**/ |
|||
@Override |
|||
public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { |
|||
if (((HttpServletRequest) request).getMethod().equals(RequestMethod.OPTIONS.name())) { |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { |
|||
// 获取ticket,如果不存在,直接返回false
|
|||
String ticket = getRequestTicket((HttpServletRequest) request); |
|||
if (StringUtils.isEmpty(ticket)) { |
|||
return false; |
|||
} |
|||
return this.executeLogin(request, response); |
|||
} |
|||
|
|||
/** |
|||
* 获取请求的ticket |
|||
*/ |
|||
private String getRequestTicket(HttpServletRequest httpRequest) { |
|||
// 从参数中获取ticket
|
|||
String ticket = httpRequest.getParameter(TICKET_PARAMETER); |
|||
if (StringUtils.isEmpty(ticket)) { |
|||
// 如果为空的话,则从header中获取参数
|
|||
ticket = httpRequest.getHeader(TICKET_PARAMETER); |
|||
} |
|||
return ticket; |
|||
} |
|||
|
|||
} |
|||
//package com.zdxt.auth.framework.shiro.web.filter;
|
|||
//
|
|||
//import org.apache.shiro.authc.AuthenticationToken;
|
|||
//import org.apache.shiro.cas.CasToken;
|
|||
//import org.slf4j.Logger;
|
|||
//import org.slf4j.LoggerFactory;
|
|||
//import org.springframework.util.StringUtils;
|
|||
//import org.springframework.web.bind.annotation.RequestMethod;
|
|||
//
|
|||
//import javax.servlet.ServletRequest;
|
|||
//import javax.servlet.ServletResponse;
|
|||
//import javax.servlet.http.HttpServletRequest;
|
|||
//
|
|||
///**
|
|||
// * cas单点登录过滤器
|
|||
// *
|
|||
// * @author: QingWang.Li
|
|||
// * @date: 2020/5/14 9:13
|
|||
// */
|
|||
//public class CasFilter extends org.apache.shiro.cas.CasFilter {
|
|||
// private static Logger logger = LoggerFactory.getLogger(CasFilter.class);
|
|||
// private static final String TICKET_PARAMETER = "ticket";
|
|||
//
|
|||
// public CasFilter() {
|
|||
// }
|
|||
//
|
|||
// @Override
|
|||
// public AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {
|
|||
// // 获取请求的ticket
|
|||
// HttpServletRequest httpRequest = (HttpServletRequest) request;
|
|||
// String ticket = getRequestTicket(httpRequest);
|
|||
// if (StringUtils.isEmpty(ticket)) {
|
|||
// logger.debug("票证获取失败,票证为空!");
|
|||
// return null;
|
|||
// }
|
|||
// return new CasToken(ticket);
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 拒绝除了option以外的所有请求
|
|||
// **/
|
|||
// @Override
|
|||
// public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
|
|||
// if (((HttpServletRequest) request).getMethod().equals(RequestMethod.OPTIONS.name())) {
|
|||
// return true;
|
|||
// }
|
|||
// return false;
|
|||
// }
|
|||
//
|
|||
// @Override
|
|||
// public boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
|
|||
// // 获取ticket,如果不存在,直接返回false
|
|||
// String ticket = getRequestTicket((HttpServletRequest) request);
|
|||
// if (StringUtils.isEmpty(ticket)) {
|
|||
// return false;
|
|||
// }
|
|||
// return this.executeLogin(request, response);
|
|||
// }
|
|||
//
|
|||
// /**
|
|||
// * 获取请求的ticket
|
|||
// */
|
|||
// private String getRequestTicket(HttpServletRequest httpRequest) {
|
|||
// // 从参数中获取ticket
|
|||
// String ticket = httpRequest.getParameter(TICKET_PARAMETER);
|
|||
// if (StringUtils.isEmpty(ticket)) {
|
|||
// // 如果为空的话,则从header中获取参数
|
|||
// ticket = httpRequest.getHeader(TICKET_PARAMETER);
|
|||
// }
|
|||
// return ticket;
|
|||
// }
|
|||
//
|
|||
//}
|
|||
|
|||
Loading…
Reference in new issue