Spring Boot学习分享(二)——整合MyBatis+Shiro

整合MyBatis以及Shiro


依旧是使用maven管理项目
pom.xml

<shiro.version>1.3.0shiro.version>

		<dependency>
			<groupId>org.apache.shirogroupId>
			<artifactId>shiro-coreartifactId>
			<version>${shiro.version}version>
		dependency>
		<dependency>
			<groupId>org.apache.shirogroupId>
			<artifactId>shiro-ehcacheartifactId>
			<version>${shiro.version}version>
		dependency>
		<dependency>
			<groupId>org.apache.shirogroupId>
			<artifactId>shiro-springartifactId>
			<version>${shiro.version}version>
		dependency>

		<dependency>
			<groupId>org.apache.shirogroupId>
			<artifactId>shiro-webartifactId>
			<version>${shiro.version}version>
		dependency>
		
		
		
		<dependency>
			<groupId>net.sf.ehcachegroupId>
			<artifactId>ehcache-coreartifactId>
			<version>2.4.8version>
		dependency>


mybatis的整合十分简单,直接在application.yml文件里面添加即可

spring: 
<!--配置数据源 -->
  datasource: 
    url: jdbc:mysql://localhost/student?useUnicode=true&characterEncoding=UTF-8&useSSL=true
    username:     
    password: 
    driver-class-name: com.mysql.jdbc.Driver
<!-- mybatis配置 -->
mybatis:
<!-- mybatis配置mapper对应的xml -->
  mapper-locations: classpath:mapper/*.xml
  
  type-aliases-package: com.example.demo.domain

shiro的整合则需要自己使用@Configuration标签注册一个类,(@Configuration相当于spring.xml里面的标签),再用@Bean注册各种实体类(@Bean相当于)
ShiroConfiguration .class

/**
 * @author MDY
 * shiro所需的配置类
 */
@Configuration
public class ShiroConfiguration {

	// 配置过滤器
	@Bean(name = "shiroFilter")
	public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manager) {
		// shiro的过滤器
		ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
		// 拦截器的配置
		// Map filter = new HashMap();
		// bean.setFilters(filter);
		// 安全管理器
		bean.setSecurityManager(manager);
		// 过滤器链配置,根据需求自己配置
		// 配置登录的url和登录成功的url
		bean.setLoginUrl("/login");
		bean.setSuccessUrl("/home");
		bean.setUnauthorizedUrl("/login?error=unidentified");
		// 配置访问权限
		LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
		// 登陆
		filterChainDefinitionMap.put("/login", "anon");
		filterChainDefinitionMap.put("/UserLogin**", "anon");
		// 验证码刷新
		filterChainDefinitionMap.put("/setImage**", "anon");
		// 用户注册
		filterChainDefinitionMap.put("/register", "anon");
		filterChainDefinitionMap.put("/registerUser**", "anon");
		// 静态文件
		filterChainDefinitionMap.put("/css/**", "anon");
		filterChainDefinitionMap.put("/js/**", "anon");
		filterChainDefinitionMap.put("/img/**", "anon");
		// 登出
		filterChainDefinitionMap.put("/logout", "logout");
		// 查看学生信息需要教师身份
		filterChainDefinitionMap.put("/manager**", "roles[teacher]");
		filterChainDefinitionMap.put("/manager/delStudent**", "roles[admin]");
		// rememberME功能实现
		filterChainDefinitionMap.put("/**", "user");
		bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
		return bean;
	}

	// 配置核心安全事务管理器
	@Bean(name = "securityManager")
	public SecurityManager securityManager(@Qualifier("myRealm") MyRealm myRealm,
			@Qualifier("rememberMeManager") CookieRememberMeManager rememberMeManager) {
		DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
		manager.setRealm(myRealm);
		manager.setRememberMeManager(rememberMeManager);
		return manager;
	}

	// 配置自定义的权限登录器
	@Bean(name = "myRealm")
	public MyRealm myRealm(@Qualifier("credentialsMatcher") CredentialsMatcher matcher) {
		MyRealm myRealm = new MyRealm();
		myRealm.setCredentialsMatcher(matcher);
		return myRealm;
	}

	// 配置自定义的密码比较器
	@Bean(name = "credentialsMatcher")
	public CredentialsMatcher credentialsMatcher(@Qualifier("cacheManager") CacheManager cacheManager) {
		RetryLimitHashedCredentialsMatcher retryLimitHashedCredentialsMatcher = new RetryLimitHashedCredentialsMatcher(
				cacheManager);
		retryLimitHashedCredentialsMatcher.setHashAlgorithmName("MD5");
		retryLimitHashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
		retryLimitHashedCredentialsMatcher.setHashIterations(2);
		return retryLimitHashedCredentialsMatcher;
	}

	// 缓存管理器
	@Bean(name = "cacheManager")
	public CacheManager cacheManager() {
		EhCacheManager cacheManager = new EhCacheManager();
		cacheManager.setCacheManagerConfigFile("classpath:shiro/ehcache.xml");
		return cacheManager;
	}

	// 配置Cookie模板
	@Bean(name = "sessionIdCookie")
	public SimpleCookie sessionIdCookie() {
		SimpleCookie sessionIdCookie = new SimpleCookie("sid");
		sessionIdCookie.setHttpOnly(true);
		sessionIdCookie.setMaxAge(-1);
		return sessionIdCookie;
	}

	@Bean(name = "rememberMeCookie")
	public SimpleCookie rememberMeCookie() {
		SimpleCookie rememberMeCookie = new SimpleCookie("rememberMe");
		rememberMeCookie.setHttpOnly(true);
		rememberMeCookie.setMaxAge(604800);
		return rememberMeCookie;
	}

	// RememberMe管理器
	@Bean(name = "rememberMeManager")
	public CookieRememberMeManager rememberMeManager(@Qualifier("rememberMeCookie") SimpleCookie rememberMeCookie) {
		CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
		rememberMeManager.setCipherKey(Base64.decode("4AvVhmFLUs0KTA3Kprsdag=="));
		rememberMeManager.setCookie(rememberMeCookie);
		return rememberMeManager;
	}

	@Bean
	public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
		return new LifecycleBeanPostProcessor();
	}

	@Bean
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
		DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
		creator.setProxyTargetClass(true);
		return creator;
	}

	@Bean
	public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
			@Qualifier("securityManager") SecurityManager manager) {
		AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
		advisor.setSecurityManager(manager);
		return advisor;
	}
}

总结:spring boot整合其它框架实际上就是将在spring中使用的xml文件使用注解转化成类,从而实现自动配置。

你可能感兴趣的:(shiro,spring-boot,mybatis,javaweb)