| @@ -41,12 +41,12 @@ public class ShiroConfig { | |||||
| @Value("${spring.redis.timeout}") | @Value("${spring.redis.timeout}") | ||||
| private int timeout; | private int timeout; | ||||
| @Value("${spring.redis.expire}") | @Value("${spring.redis.expire}") | ||||
| private int expire; | private int expire; | ||||
| @Value("${spring.redis.password}") | |||||
| private String password; | |||||
| @Value("${spring.redis.password}") | |||||
| private String password; | |||||
| @Bean | @Bean | ||||
| public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() { | public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() { | ||||
| @@ -61,44 +61,44 @@ public class ShiroConfig { | |||||
| // public ShiroDialect shiroDialect() { | // public ShiroDialect shiroDialect() { | ||||
| // return new ShiroDialect(); | // return new ShiroDialect(); | ||||
| // } | // } | ||||
| /** | /** | ||||
| * ShiroFilterFactoryBean 处理拦截资源文件问题。 | * ShiroFilterFactoryBean 处理拦截资源文件问题。 | ||||
| * 注意:单独一个ShiroFilterFactoryBean配置是或报错的,因为在 | * 注意:单独一个ShiroFilterFactoryBean配置是或报错的,因为在 | ||||
| * 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager | * 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager | ||||
| * | |||||
| Filter Chain定义说明 | |||||
| 1、一个URL可以配置多个Filter,使用逗号分隔 | |||||
| 2、当设置多个过滤器时,全部验证通过,才视为通过 | |||||
| 3、部分过滤器可指定参数,如perms,roles | |||||
| * | |||||
| * <p> | |||||
| * Filter Chain定义说明 | |||||
| * 1、一个URL可以配置多个Filter,使用逗号分隔 | |||||
| * 2、当设置多个过滤器时,全部验证通过,才视为通过 | |||||
| * 3、部分过滤器可指定参数,如perms,roles | |||||
| */ | */ | ||||
| @Bean | @Bean | ||||
| public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager){ | |||||
| public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { | |||||
| System.out.println("ShiroConfiguration.shirFilter()"); | System.out.println("ShiroConfiguration.shirFilter()"); | ||||
| ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); | |||||
| ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); | |||||
| // 必须设置 SecurityManager | // 必须设置 SecurityManager | ||||
| shiroFilterFactoryBean.setSecurityManager(securityManager); | shiroFilterFactoryBean.setSecurityManager(securityManager); | ||||
| Map<String, Filter> filters = new LinkedHashMap<String, Filter>(); | Map<String, Filter> filters = new LinkedHashMap<String, Filter>(); | ||||
| filters.put("token", new ShiroLoginFilter()); | |||||
| filters.put("corsFilter", new RestFilter()); | |||||
| shiroFilterFactoryBean.setFilters(filters); | |||||
| filters.put("token", new ShiroLoginFilter()); | |||||
| filters.put("corsFilter", new RestFilter()); | |||||
| shiroFilterFactoryBean.setFilters(filters); | |||||
| // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 | // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 | ||||
| shiroFilterFactoryBean.setLoginUrl("/#/"); | shiroFilterFactoryBean.setLoginUrl("/#/"); | ||||
| // 登录成功后要跳转的链接 | // 登录成功后要跳转的链接 | ||||
| shiroFilterFactoryBean.setSuccessUrl("/usersPage"); | shiroFilterFactoryBean.setSuccessUrl("/usersPage"); | ||||
| //未授权界面; | //未授权界面; | ||||
| shiroFilterFactoryBean.setUnauthorizedUrl("/403"); | shiroFilterFactoryBean.setUnauthorizedUrl("/403"); | ||||
| //拦截器. | //拦截器. | ||||
| Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); | |||||
| Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>(); | |||||
| //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 | //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 | ||||
| filterChainDefinitionMap.put("/logout", "anon"); | filterChainDefinitionMap.put("/logout", "anon"); | ||||
| filterChainDefinitionMap.put("/doLogin/**","anon"); | |||||
| filterChainDefinitionMap.put("/css/**","anon"); | |||||
| filterChainDefinitionMap.put("/js/**","anon"); | |||||
| filterChainDefinitionMap.put("/img/**","anon"); | |||||
| filterChainDefinitionMap.put("/font-awesome/**","anon"); | |||||
| filterChainDefinitionMap.put("/doLogin/**", "anon"); | |||||
| filterChainDefinitionMap.put("/css/**", "anon"); | |||||
| filterChainDefinitionMap.put("/js/**", "anon"); | |||||
| filterChainDefinitionMap.put("/img/**", "anon"); | |||||
| filterChainDefinitionMap.put("/font-awesome/**", "anon"); | |||||
| //<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; | //<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; | ||||
| //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> | //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> | ||||
| //自定义加载权限资源关系 | //自定义加载权限资源关系 | ||||
| @@ -111,17 +111,17 @@ public class ShiroConfig { | |||||
| // filterChainDefinitionMap.put(resources.getUrl(),permission); | // filterChainDefinitionMap.put(resources.getUrl(),permission); | ||||
| // } | // } | ||||
| // } | // } | ||||
| filterChainDefinitionMap.put("/swagger-ui.html","anon"); | |||||
| filterChainDefinitionMap.put("/swagger-ui.html", "anon"); | |||||
| filterChainDefinitionMap.put("/wxPay/notify/**", "anon"); | filterChainDefinitionMap.put("/wxPay/notify/**", "anon"); | ||||
| filterChainDefinitionMap.put("/v2/**","anon"); | |||||
| filterChainDefinitionMap.put("/swagger-resources/**","anon"); | |||||
| filterChainDefinitionMap.put("/webjars/**","anon"); | |||||
| filterChainDefinitionMap.put("/wxMsgCallback/**","anon"); | |||||
| filterChainDefinitionMap.put("/user/sendvalidationcode","anon"); | |||||
| filterChainDefinitionMap.put("/user/updatepwd","anon"); | |||||
| filterChainDefinitionMap.put("/carCallback/**","anon"); | |||||
| filterChainDefinitionMap.put("/wxMallApply/add","anon"); | |||||
| filterChainDefinitionMap.put("/wxMallApply/sendvalidationcode","anon"); | |||||
| filterChainDefinitionMap.put("/v2/**", "anon"); | |||||
| filterChainDefinitionMap.put("/swagger-resources/**", "anon"); | |||||
| filterChainDefinitionMap.put("/webjars/**", "anon"); | |||||
| filterChainDefinitionMap.put("/wxMsgCallback/**", "anon"); | |||||
| filterChainDefinitionMap.put("/user/sendvalidationcode", "anon"); | |||||
| filterChainDefinitionMap.put("/user/updatepwd", "anon"); | |||||
| filterChainDefinitionMap.put("/carCallback/**", "anon"); | |||||
| filterChainDefinitionMap.put("/wxMallApply/add", "anon"); | |||||
| filterChainDefinitionMap.put("/wxMallApply/sendvalidationcode", "anon"); | |||||
| filterChainDefinitionMap.put("/captcha.jpg", "anon"); | filterChainDefinitionMap.put("/captcha.jpg", "anon"); | ||||
| filterChainDefinitionMap.put("/version", "anon"); | filterChainDefinitionMap.put("/version", "anon"); | ||||
| // filterChainDefinitionMap.put("/role/**", "corsFilter,token"); | // filterChainDefinitionMap.put("/role/**", "corsFilter,token"); | ||||
| @@ -129,27 +129,24 @@ public class ShiroConfig { | |||||
| // filterChainDefinitionMap.put("/**", "anon"); | // filterChainDefinitionMap.put("/**", "anon"); | ||||
| shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); | shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); | ||||
| return shiroFilterFactoryBean; | return shiroFilterFactoryBean; | ||||
| } | } | ||||
| public static boolean isAjax(ServletRequest request){ | |||||
| public static boolean isAjax(ServletRequest request) { | |||||
| String header = ((HttpServletRequest) request).getHeader("X-Requested-With"); | String header = ((HttpServletRequest) request).getHeader("X-Requested-With"); | ||||
| if("XMLHttpRequest".equalsIgnoreCase(header)){ | |||||
| System.out.println( "当前请求为Ajax请求"); | |||||
| if ("XMLHttpRequest".equalsIgnoreCase(header)) { | |||||
| System.out.println("当前请求为Ajax请求"); | |||||
| return Boolean.TRUE; | return Boolean.TRUE; | ||||
| } | } | ||||
| System.out.println( "当前请求非Ajax请求"); | |||||
| System.out.println("当前请求非Ajax请求"); | |||||
| return Boolean.FALSE; | return Boolean.FALSE; | ||||
| } | } | ||||
| @Bean | @Bean | ||||
| public SecurityManager securityManager(){ | |||||
| DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); | |||||
| public SecurityManager securityManager() { | |||||
| DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); | |||||
| //设置realm. | //设置realm. | ||||
| securityManager.setRealm(myShiroRealm()); | securityManager.setRealm(myShiroRealm()); | ||||
| // 自定义缓存实现 使用redis | // 自定义缓存实现 使用redis | ||||
| @@ -160,23 +157,23 @@ public class ShiroConfig { | |||||
| } | } | ||||
| @Bean | @Bean | ||||
| public MyShiroRealm myShiroRealm(){ | |||||
| public MyShiroRealm myShiroRealm() { | |||||
| MyShiroRealm myShiroRealm = new MyShiroRealm(); | MyShiroRealm myShiroRealm = new MyShiroRealm(); | ||||
| myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); | myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); | ||||
| return myShiroRealm; | return myShiroRealm; | ||||
| } | } | ||||
| /** | /** | ||||
| * 凭证匹配器 | * 凭证匹配器 | ||||
| * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 | * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 | ||||
| * 所以我们需要修改下doGetAuthenticationInfo中的代码; | |||||
| * 所以我们需要修改下doGetAuthenticationInfo中的代码; | |||||
| * ) | * ) | ||||
| * | |||||
| * @return | * @return | ||||
| */ | */ | ||||
| @Bean | @Bean | ||||
| public HashedCredentialsMatcher hashedCredentialsMatcher(){ | |||||
| public HashedCredentialsMatcher hashedCredentialsMatcher() { | |||||
| HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); | HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); | ||||
| hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; | hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; | ||||
| @@ -187,13 +184,14 @@ public class ShiroConfig { | |||||
| /** | /** | ||||
| * 开启shiro aop注解支持. | |||||
| * 使用代理方式;所以需要开启代码支持; | |||||
| * 开启shiro aop注解支持. | |||||
| * 使用代理方式;所以需要开启代码支持; | |||||
| * | |||||
| * @param securityManager | * @param securityManager | ||||
| * @return | * @return | ||||
| */ | */ | ||||
| @Bean | @Bean | ||||
| public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ | |||||
| public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) { | |||||
| AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); | AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); | ||||
| authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); | authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); | ||||
| return authorizationAttributeSourceAdvisor; | return authorizationAttributeSourceAdvisor; | ||||
| @@ -202,6 +200,7 @@ public class ShiroConfig { | |||||
| /** | /** | ||||
| * 配置shiro redisManager | * 配置shiro redisManager | ||||
| * 使用的是shiro-redis开源插件 | * 使用的是shiro-redis开源插件 | ||||
| * | |||||
| * @return | * @return | ||||
| */ | */ | ||||
| public RedisManager redisManager() { | public RedisManager redisManager() { | ||||
| @@ -217,6 +216,7 @@ public class ShiroConfig { | |||||
| /** | /** | ||||
| * cacheManager 缓存 redis实现 | * cacheManager 缓存 redis实现 | ||||
| * 使用的是shiro-redis开源插件 | * 使用的是shiro-redis开源插件 | ||||
| * | |||||
| * @return | * @return | ||||
| */ | */ | ||||
| public RedisCacheManager cacheManager() { | public RedisCacheManager cacheManager() { | ||||
| @@ -238,7 +238,7 @@ public class ShiroConfig { | |||||
| } | } | ||||
| /** | /** | ||||
| * shiro session的管理 | |||||
| * shiro session的管理 | |||||
| */ | */ | ||||
| @Bean | @Bean | ||||
| public DefaultWebSessionManager sessionManager() { | public DefaultWebSessionManager sessionManager() { | ||||
| @@ -252,14 +252,14 @@ public class ShiroConfig { | |||||
| sessionManager.setSessionIdCookie(simpleCookie()); | sessionManager.setSessionIdCookie(simpleCookie()); | ||||
| return sessionManager; | return sessionManager; | ||||
| } | } | ||||
| @Bean | @Bean | ||||
| public SimpleCookie simpleCookie() { | public SimpleCookie simpleCookie() { | ||||
| SimpleCookie simpleCookie = new SimpleCookie("SSIDS"); | |||||
| SimpleCookie simpleCookie = new SimpleCookie("SSIDS"); | |||||
| simpleCookie.setDomain(""); | simpleCookie.setDomain(""); | ||||
| return simpleCookie; | |||||
| return simpleCookie; | |||||
| } | } | ||||
| // @Bean | // @Bean | ||||
| // public SimpleCookie rememberMeCookie(){ | // public SimpleCookie rememberMeCookie(){ | ||||
| // //System.out.println("ShiroConfiguration.rememberMeCookie()"); | // //System.out.println("ShiroConfiguration.rememberMeCookie()"); | ||||
| @@ -269,7 +269,7 @@ public class ShiroConfig { | |||||
| // simpleCookie.setMaxAge(60*30); | // simpleCookie.setMaxAge(60*30); | ||||
| // return simpleCookie; | // return simpleCookie; | ||||
| // } | // } | ||||
| // @Bean | // @Bean | ||||
| // public CookieRememberMeManager rememberMeManager(){ | // public CookieRememberMeManager rememberMeManager(){ | ||||
| // //System.out.println("ShiroConfiguration.rememberMeManager()"); | // //System.out.println("ShiroConfiguration.rememberMeManager()"); | ||||
| @@ -279,7 +279,7 @@ public class ShiroConfig { | |||||
| // cookieRememberMeManager.setCipherKey(Base64.decodeBytes("2AvVhdsgUs0FSA3SDFAdag==")); | // cookieRememberMeManager.setCipherKey(Base64.decodeBytes("2AvVhdsgUs0FSA3SDFAdag==")); | ||||
| // return cookieRememberMeManager; | // return cookieRememberMeManager; | ||||
| // } | // } | ||||
| // @Bean(name = "securityManager") | // @Bean(name = "securityManager") | ||||
| // public DefaultWebSecurityManager defaultWebSecurityManager(MyShiroRealm realm){ | // public DefaultWebSecurityManager defaultWebSecurityManager(MyShiroRealm realm){ | ||||
| // DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); | // DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); | ||||