本文所提供的项目实例,是我将公司项目中的shiro代码进行了抽取、整理并添加了一些注释而形成的。
所以例子中并不包含shiro所有的功能,但是本系列文章前9篇所讲解的内容在这里都是可以找到的。
本示例项目所使用的技术如下:
集成开发环境为IDEA,项目构建使用spring boot,包管理使用maven,页面展示使用freemaker,控制层使用spring mvc等。
在本篇博文中会贴出主要代码,完整的项目已经上传到码云大家可以下载查看使用。
项目码云地址:http://git.oschina.net/imlichao/shiro-example
项目结构
freemaker配置文件
package pub.lichao.shiro.config;
import com.jagregory.shiro.freemarker.ShiroTags;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.util.HashMap;
import java.util.Map;
/**
* FreeMarker配置文件
*/
@Configuration
public class FreemarkerConfig {
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer(FreeMarkerProperties freeMarkerProperties) {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPaths(freeMarkerProperties.getTemplateLoaderPath()); //模板加载路径默认 "classpath:/templates/"
configurer.setDefaultEncoding("utf-8");//设置页面默认编码(不设置页面中文乱码)
Map<String,Object> variables=new HashMap<String,Object>();
variables.put("shiro", new ShiroTags());
configurer.setFreemarkerVariables(variables);//添加shiro自定义标签
return configurer;
}
}
shiro配置文件
package pub.lichao.shiro.config;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.codec.Base64;
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.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;
import pub.lichao.shiro.shiro.AuthenticationFilter;
import pub.lichao.shiro.shiro.ShiroRealm;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Shiro配置
*/
@Configuration
public class ShiroConfig {
/**
* 创建EhCache缓存类
* @return
*/
@Bean(name = "shiroCacheManager")
public EhCacheManager shiroCacheManager() {
EhCacheManager ehCacheManager = new EhCacheManager();
ehCacheManager.setCacheManagerConfigFile("classpath:shiro-ehcache.xml");//指定缓存配置文件路径
return ehCacheManager;
}
/**
* 创建安全认证资源类
* (自己实现的登陆和授权认证规则)
*/
@Bean(name = "shiroRealm")
public ShiroRealm shiroRealm(EhCacheManager shiroCacheManager) {
ShiroRealm realm = new ShiroRealm();
realm.setCacheManager(shiroCacheManager); //为资源类配置缓存
return realm;
}
/**
* 创建保存记住我信息的Cookie
*/
@Bean(name = "rememberMeCookie")
public SimpleCookie getSimpleCookie() {
SimpleCookie simpleCookie = new SimpleCookie();
simpleCookie.setName("rememberMe");//cookie名字
simpleCookie.setHttpOnly(true); //设置cookieHttpOnly,保证cookie安全
simpleCookie.setMaxAge(604800); //保存7天 单位秒
return simpleCookie;
}
/**
* 创建记住我管理器
*/
@Bean(name = "rememberMeManager")
public CookieRememberMeManager getCookieRememberMeManager(SimpleCookie rememberMeCookie) {
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
byte[] cipherKey = Base64.decode("wGiHplamyXlVB11UXWol8g==");//创建cookie秘钥
cookieRememberMeManager.setCipherKey(cipherKey); //存入cookie秘钥
cookieRememberMeManager.setCookie(rememberMeCookie); //存入记住我Cookie
return cookieRememberMeManager;
}
/**
* 创建默认的安全管理类
* 整个安全认证流程的管理都由此类负责
*/
@Bean(name = "securityManager")
public DefaultWebSecurityManager securityManager(ShiroRealm shiroRealm,EhCacheManager shiroCacheManager,CookieRememberMeManager rememberMeManager) {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager(); //创建安全管理类
defaultWebSecurityManager.setRealm(shiroRealm); //指定资源类
defaultWebSecurityManager.setCacheManager(shiroCacheManager);//为管理类配置Session缓存
defaultWebSecurityManager.setRememberMeManager(rememberMeManager);//配置记住我cookie管理类
return defaultWebSecurityManager;
}
/**
* 获得拦截器工厂类
*/
@Bean (name = "authenticationFilter")
public AuthenticationFilter authenticationFilter() {
return new AuthenticationFilter();
}
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager,AuthenticationFilter authenticationFilter) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);//设置SecurityManager,必输
shiroFilterFactoryBean.setLoginUrl("/login");