springboot1.5.9 + mybatis + layui + shiro后臺(tái)權(quán)限管理系統(tǒng)

后臺(tái)管理系統(tǒng)

業(yè)務(wù)場(chǎng)景

spring boot + mybatis后臺(tái)管理系統(tǒng)框架;

layUI前端界面;

shiro權(quán)限控制,ehCache緩存;

開發(fā)背景

maven :3.3.3?

JDK : 1.8?

Intellij IDEA : 2017.2.5 開發(fā)工具?

spring boot :1.5.9.RELEASE?

mybatis 3.4.5 :dao層框架?

pageHelper : 5.1.2?

httpClient : 4.5.3

layui 2.2.3 :前端框架?

shiro 1.4.0 :權(quán)限控制框架?

druid 1.1.5 :druid連接池,監(jiān)控?cái)?shù)據(jù)庫(kù)性能,記錄SQL執(zhí)行日志?

thymeleaf :2.1.4.RELEASE,thymeleaf前端html頁(yè)面模版?

log4j2 2.7 :日志框架?

EHCache : 2.5.0?

ztree : 3.5.31

項(xiàng)目框架

spring boot + mybatis + shiro + layui + ehcache?

項(xiàng)目源碼:(包含數(shù)據(jù)庫(kù)源碼)?

github源碼:?https://github.com/wyait/manage.git?

碼云:https://gitee.com/wyait/manage.git

基礎(chǔ)框架

spring boot + mybatis的整合,參考博客:?

http://blog.51cto.com/wyait/1969626

整合layui

layui官網(wǎng):http://www.layui.com?

layui下載地址:https://github.com/sentsin/layui/

將下載的layui解壓后,復(fù)制到項(xiàng)目的static/目錄下:?

在templates/目錄下,新建index.html,根據(jù)layui官網(wǎng)的API(后臺(tái)布局代碼),引入相關(guān)代碼:?

==注意:?

html頁(yè)面中的標(biāo)簽必須要加上對(duì)應(yīng)的閉合標(biāo)簽或標(biāo)簽內(nèi)加上"/",比如: 或 等;?

在引入static/目錄下的css和js等文件時(shí),路徑中不需要加"/static/",默認(rèn)加載的是static/目錄下的文件;==

整合shiro權(quán)限控制

shiro簡(jiǎn)介

Apache Shiro是一個(gè)功能強(qiáng)大、靈活的,開源的安全框架。它可以干凈利落地處理身份驗(yàn)證、授權(quán)、企業(yè)會(huì)話管理和加密。

Apache Shiro的首要目標(biāo)是易于使用和理解。安全通常很復(fù)雜,甚至讓人感到很痛苦,但是Shiro卻不是這樣子的。一個(gè)好的安全框架應(yīng)該屏蔽復(fù)雜性,向外暴露簡(jiǎn)單、直觀的API,來(lái)簡(jiǎn)化開發(fā)人員實(shí)現(xiàn)應(yīng)用程序安全所花費(fèi)的時(shí)間和精力。

Shiro能做什么呢?

驗(yàn)證用戶身份

用戶訪問權(quán)限控制,比如:1、判斷用戶是否分配了一定的安全角色。2、判斷用戶是否被授予完成某個(gè)操作的權(quán)限

在非 web 或 EJB 容器的環(huán)境下可以任意使用Session API

可以響應(yīng)認(rèn)證、訪問控制,或者 Session 生命周期中發(fā)生的事件

可將一個(gè)或以上用戶安全數(shù)據(jù)源數(shù)據(jù)組合成一個(gè)復(fù)合的用戶 "view"(視圖)

支持單點(diǎn)登錄(SSO)功能

支持提供“Remember Me”服務(wù),獲取用戶關(guān)聯(lián)信息而無(wú)需登錄

等等——都集成到一個(gè)有凝聚力的易于使用的API。根據(jù)官方的介紹,shiro提供了“身份認(rèn)證”、“授權(quán)”、“加密”和“Session管理”這四個(gè)主要的核心功能

// TODO 百度

引入依賴

pom.xml中引入shiro依賴:

org.apache.shiroshiro-spring${shiro.version}org.apache.shiroshiro-all${shiro.version}

shiro.version版本為:1.3.1

shiro配置實(shí)體類

/** * @項(xiàng)目名稱:wyait-manage * @包名:com.wyait.manage.config * @類描述: * @創(chuàng)建人:wyait * @創(chuàng)建時(shí)間:2017-12-12 18:51 *@version:V1.0 */@ConfigurationpublicclassShiroConfig{privatestaticfinalLogger logger = LoggerFactory? ? ? ? ? ? .getLogger(ShiroConfig.class);/**

? ? * ShiroFilterFactoryBean 處理攔截資源文件過濾器

? ? *?
1,配置shiro安全管理器接口securityManage;

? ? *?
2,shiro 連接約束配置filterChainDefinitions;

? ? */@BeanpublicShiroFilterFactoryBeanshiroFilterFactoryBean(

? ? ? ? ? ? org.apache.shiro.mgt.SecurityManager securityManager){//shiroFilterFactoryBean對(duì)象ShiroFilterFactoryBean shiroFilterFactoryBean =newShiroFilterFactoryBean();// 配置shiro安全管理器 SecurityManagershiroFilterFactoryBean.setSecurityManager(securityManager);// 指定要求登錄時(shí)的鏈接shiroFilterFactoryBean.setLoginUrl("/login");// 登錄成功后要跳轉(zhuǎn)的鏈接shiroFilterFactoryBean.setSuccessUrl("/index");// 未授權(quán)時(shí)跳轉(zhuǎn)的界面;shiroFilterFactoryBean.setUnauthorizedUrl("/403");// filterChainDefinitions攔截器Map filterChainDefinitionMap =newLinkedHashMap();// 配置不會(huì)被攔截的鏈接 從上向下順序判斷filterChainDefinitionMap.put("/static/**","anon");? ? ? ? filterChainDefinitionMap.put("/templates/**","anon");// 配置退出過濾器,具體的退出代碼Shiro已經(jīng)替我們實(shí)現(xiàn)了filterChainDefinitionMap.put("/logout","logout");//add操作,該用戶必須有【addOperation】權(quán)限filterChainDefinitionMap.put("/add","perms[addOperation]");// filterChainDefinitionMap.put("/user/**","authc");? ? ? ? shiroFilterFactoryBean? ? ? ? ? ? ? ? .setFilterChainDefinitionMap(filterChainDefinitionMap);? ? ? ? logger.debug("Shiro攔截器工廠類注入成功");returnshiroFilterFactoryBean;? ? }/**? ? * shiro安全管理器設(shè)置realm認(rèn)證? ? *@return*/@Beanpublicorg.apache.shiro.mgt.SecurityManagersecurityManager(){? ? ? ? DefaultWebSecurityManager securityManager =newDefaultWebSecurityManager();// 設(shè)置realm.securityManager.setRealm(shiroRealm());// //注入ehcache緩存管理器;securityManager.setCacheManager(ehCacheManager());returnsecurityManager;? ? }/**? ? * 身份認(rèn)證realm; (賬號(hào)密碼校驗(yàn);權(quán)限等)? ? *? ? *@return*/@BeanpublicShiroRealmshiroRealm(){? ? ? ? ShiroRealm shiroRealm =newShiroRealm();returnshiroRealm;? ? }/**? ? * ehcache緩存管理器;shiro整合ehcache:? ? * 通過安全管理器:securityManager? ? *@returnEhCacheManager? ? */@BeanpublicEhCacheManagerehCacheManager(){? ? ? ? logger.debug("=====shiro整合ehcache緩存:ShiroConfiguration.getEhCacheManager()");? ? ? ? EhCacheManager cacheManager =newEhCacheManager();? ? ? ? cacheManager.setCacheManagerConfigFile("classpath:config/ehcache.xml");returncacheManager;? ? }}

Filter Chain定義說(shuō)明:

1、一個(gè)URL可以配置多個(gè)Filter,使用逗號(hào)分隔;?

2、當(dāng)設(shè)置多個(gè)過濾器時(shí),全部驗(yàn)證通過,才視為通過;?

3、部分過濾器可指定參數(shù),如perms,roles

Shiro內(nèi)置的FilterChain:

Filter NameClass

anonorg.apache.shiro.web.filter.authc.AnonymousFilter

authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter

authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter

permsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter

portorg.apache.shiro.web.filter.authz.PortFilter

restorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilter

rolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFilter

sslorg.apache.shiro.web.filter.authz.SslFilter

userorg.apache.shiro.web.filter.authc.UserFilter

anon : 所有url都都可以匿名訪問?

authc : 需要認(rèn)證才能進(jìn)行訪問?

user : 配置記住我或認(rèn)證通過可以訪問

ShiroRealm認(rèn)證實(shí)體類

/** * @項(xiàng)目名稱:wyait-manage * @包名:com.wyait.manage.shiro * @類描述: * @創(chuàng)建人:wyait * @創(chuàng)建時(shí)間:2017-12-13 13:53 *@version:V1.0 */publicclassShiroRealmextendsAuthorizingRealm{@OverrideprotectedAuthorizationInfodoGetAuthorizationInfo(

? ? ? ? ? ? PrincipalCollection principalCollection){//TODOreturnnull;? ? }@OverrideprotectedAuthenticationInfodoGetAuthenticationInfo(

? ? ? ? ? ? AuthenticationToken authenticationToken)throwsAuthenticationException{//TODOreturnnull;? ? }}

shiro使用ehcache緩存

導(dǎo)入依賴;

org.apache.shiroshiro-ehcache1.2.6

? 包含支持UI模版(Velocity,F(xiàn)reeMarker,JasperReports),

? 郵件服務(wù),

? 腳本服務(wù)(JRuby),

? 緩存Cache(EHCache),

? 任務(wù)計(jì)劃Scheduling(uartz)。

-->org.springframeworkspring-context-support

引入ehcache.xml配置文件;

shiro配置類中整合ehcache做緩存管理;【參考:shiro配置實(shí)體類】

整合thymeleaf

導(dǎo)入pom依賴

org.springframework.bootspring-boot-starter-thymeleaf

配置中禁用緩存

#關(guān)閉thymeleaf緩存spring.thymeleaf.cache=false

shiro功能之記住我

shiro記住我的功能是基于瀏覽器中的cookie實(shí)現(xiàn)的;

在shiroConfig里面增加cookie配置

CookieRememberMeManager配置;

/*** 設(shè)置記住我cookie過期時(shí)間*@return*/@BeanpublicSimpleCookieremeberMeCookie(){logger.debug("記住我,設(shè)置cookie過期時(shí)間!");//cookie名稱;對(duì)應(yīng)前端的checkbox的name = rememberMeSimpleCookie scookie=newSimpleCookie("rememberMe");//記住我cookie生效時(shí)間1小時(shí) ,單位秒? [1小時(shí)]scookie.setMaxAge(3600);returnscookie;}

/**

配置cookie記住我管理器

@returnbr/>*/

@Bean

public CookieRememberMeManager rememberMeManager(){

logger.debug("配置cookie記住我管理器!");

CookieRememberMeManager cookieRememberMeManager=new CookieRememberMeManager();

cookieRememberMeManager.setCookie(remeberMeCookie());

return cookieRememberMeManager;

}

- 將CookieRememberMeManager注入SecurityManager

//注入Cookie記住我管理器

securityManager.setRememberMeManager(rememberMeManager());

前端頁(yè)面新增rememberMe復(fù)選框

登錄方法更改

//新增rememberMe參數(shù)@RequestParam(value="rememberMe",required =false)booleanrememberMe... ...// 1、 封裝用戶名、密碼、是否記住我到token令牌對(duì)象? [支持記住我]AuthenticationToken token =newUsernamePasswordToken(? ? ? ? ? ? user.getMobile(),? DigestUtils.md5Hex(user.getPassword()),rememberMe);

頁(yè)面cookie設(shè)置?

shiro功能之密碼錯(cuò)誤次數(shù)限制

針對(duì)用戶在登錄時(shí)用戶名和密碼輸入錯(cuò)誤進(jìn)行次數(shù)限制,并鎖定;?

Shiro中用戶名密碼的驗(yàn)證交給了CredentialsMatcher;

在CredentialsMatcher里面校驗(yàn)用戶密碼,使用ehcache記錄登錄失敗次數(shù)就可以實(shí)現(xiàn)。

在驗(yàn)證用戶名密碼之前先驗(yàn)證登錄失敗次數(shù),如果超過5次就拋出嘗試過多的異常,否則驗(yàn)證用戶名密碼,驗(yàn)證成功把嘗試次數(shù)清零,不成功則直接退出。這里依靠Ehcache自帶的timeToIdleSeconds來(lái)保證鎖定時(shí)間(帳號(hào)鎖定之后的最后一次嘗試間隔timeToIdleSeconds秒之后自動(dòng)清除)。

自定義HashedCredentialsMatcher實(shí)現(xiàn)類

/** * @項(xiàng)目名稱:lyd-channel * @包名:com.lyd.channel.shiro * @類描述:shiro之密碼輸入次數(shù)限制6次,并鎖定2分鐘 * @創(chuàng)建人:wyait * @創(chuàng)建時(shí)間:2018年1月23日17:23:10 *@version:V1.0 */publicclassRetryLimitHashedCredentialsMatcherextendsHashedCredentialsMatcher{//集群中可能會(huì)導(dǎo)致出現(xiàn)驗(yàn)證多過5次的現(xiàn)象,因?yàn)锳tomicInteger只能保證單節(jié)點(diǎn)并發(fā)//解決方案,利用ehcache、redis(記錄錯(cuò)誤次數(shù))和mysql數(shù)據(jù)庫(kù)(鎖定)的方式處理:密碼輸錯(cuò)次數(shù)限制; 或兩者結(jié)合使用privateCache passwordRetryCache;publicRetryLimitHashedCredentialsMatcher(CacheManager cacheManager){//讀取ehcache中配置的登錄限制鎖定時(shí)間passwordRetryCache = cacheManager.getCache("passwordRetryCache");? ? ? }/**? ? * 在回調(diào)方法doCredentialsMatch(AuthenticationToken token,AuthenticationInfo info)中進(jìn)行身份認(rèn)證的密碼匹配,? ? *
這里我們引入了Ehcahe用于保存用戶登錄次數(shù),如果登錄失敗retryCount變量則會(huì)一直累加,如果登錄成功,那么這個(gè)count就會(huì)從緩存中移除,? ? *
從而實(shí)現(xiàn)了如果登錄次數(shù)超出指定的值就鎖定。? ? *@paramtoken? ? *@paraminfo? ? *@return*/@OverridepublicbooleandoCredentialsMatch(AuthenticationToken token,?

? ? ? ? ? ? AuthenticationInfo info){//獲取登錄用戶名String username = (String) token.getPrincipal();//從ehcache中獲取密碼輸錯(cuò)次數(shù)// retryCountAtomicInteger retryCount = passwordRetryCache.get(username);if(retryCount ==null) {//第一次retryCount =newAtomicInteger(0);? ? ? ? ? ? ? passwordRetryCache.put(username, retryCount);? ? ? ? ? }//retryCount.incrementAndGet()自增:count + 1if(retryCount.incrementAndGet() >5) {// if retry count > 5 throw? 超過5次 鎖定thrownewExcessiveAttemptsException("username:"+username+" tried to login more than 5 times in period");? ? ? ? }//否則走判斷密碼邏輯booleanmatches =super.doCredentialsMatch(token, info);if(matches) {// clear retry count? 清楚ehcache中的count次數(shù)緩存passwordRetryCache.remove(username);? ? ? ? ? }returnmatches;? ? ? }? }

這里的邏輯也不復(fù)雜,在回調(diào)方法doCredentialsMatch(AuthenticationToken token,AuthenticationInfo info)?

中進(jìn)行身份認(rèn)證的密碼匹配,這里我們引入了Ehcahe用于保存用戶登錄次數(shù),如果登錄失敗retryCount變量則會(huì)一直累加,如果登錄成功,那么這個(gè)count就會(huì)從緩存中移除,從而實(shí)現(xiàn)了如果登錄次數(shù)超出指定的值就鎖定。

ehcache中新增密碼重試次數(shù)緩存passwordRetryCache

在shiroConfig配置類中添加HashedCredentialsMatcher憑證匹配器

/**? ? * 憑證匹配器 (由于我們的密碼校驗(yàn)交給Shiro的SimpleAuthenticationInfo進(jìn)行處理了? ? * 所以我們需要修改下doGetAuthenticationInfo中的代碼,更改密碼生成規(guī)則和校驗(yàn)的邏輯一致即可; )? ? *? ? *@return*/@BeanpublicHashedCredentialsMatcherhashedCredentialsMatcher(){? ? ? ? HashedCredentialsMatcher hashedCredentialsMatcher =newRetryLimitHashedCredentialsMatcher(ehCacheManager());//new HashedCredentialsMatcher();hashedCredentialsMatcher.setHashAlgorithmName("md5");// 散列算法:這里使用MD5算法;hashedCredentialsMatcher.setHashIterations(1);// 散列的次數(shù),比如散列兩次,相當(dāng)于 // md5(md5(""));returnhashedCredentialsMatcher;? ? }

設(shè)置ShiroRealm密碼匹配使用自定義的HashedCredentialsMatcher實(shí)現(xiàn)類

//使用自定義的CredentialsMatcher進(jìn)行密碼校驗(yàn)和輸錯(cuò)次數(shù)限制shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());

更改ShiroRealm類doGetAuthenticationInfo登錄認(rèn)證方法

更改密碼加密規(guī)則,和自定義的HashedCredentialsMatcher匹配器加密規(guī)則保持一致;

// 第一個(gè)參數(shù) ,登陸后,需要在session保存數(shù)據(jù)// 第二個(gè)參數(shù),查詢到密碼(加密規(guī)則要和自定義的HashedCredentialsMatcher中的HashAlgorithmName散列算法一致)// 第三個(gè)參數(shù) ,realm名字newSimpleAuthenticationInfo(user, DigestUtils.md5Hex(user.getPassword()),? ? ? ? ? ? ? ? ? ? getName());

login方法的改動(dòng);

controller層獲取登錄失敗次數(shù);登錄頁(yè)面新增用戶、密碼輸錯(cuò)次數(shù)提醒;

//注入ehcache管理器@AutowiredprivateEhCacheManager ecm;... ...//登錄方法中,獲取失敗次數(shù),并設(shè)置友情提示信息Cache passwordRetryCache= ecm.getCache("passwordRetryCache");if(null!=passwordRetryCache){intretryNum=(passwordRetryCache.get(existUser.getMobile())==null?0:passwordRetryCache.get(existUser.getMobile())).intValue();? ? logger.debug("輸錯(cuò)次數(shù):"+retryNum);if(retryNum>0&& retryNum<6){? ? ? ? responseResult.setMessage("用戶名或密碼錯(cuò)誤"+retryNum+"次,再輸錯(cuò)"+(6-retryNum)+"次賬號(hào)將鎖定");? ? }}

后臺(tái)新增用戶解鎖操作;清除ehcache中的緩存即可;?

TODO?

用戶列表,解鎖按鈕,點(diǎn)擊,彈出輸入框,讓用戶管理員輸入需要解鎖的用戶手機(jī)號(hào),進(jìn)行解鎖操作即可;

Cache passwordRetryCache= ecm.getCache("passwordRetryCache");//username是緩存keypasswordRetryCache..remove(username);

thymeleaf整合shiro

html頁(yè)面使用thymeleaf模版;

導(dǎo)入pom依賴

com.github.theborakompanionithymeleaf-extras-shiro1.2.1

thymeleaf整合shiro的依賴:thymeleaf-extras-shiro最新版本是2.0.0,配置使用報(bào)錯(cuò),所以使用1.2.1版本;?

該jar包的github地址:

https://github.com/theborakompanioni/thymeleaf-extras-shiro

配置shiroDirect

@BeanpublicShiroDialectshiroDialect(){returnnewShiroDialect();}

這段代碼放在ShiroConfig配置類里面即可。

頁(yè)面中使用

... ...

具體用法,參考:https://github.com/theborakompanioni/thymeleaf-extras-shiro

整合pageHelper

導(dǎo)入pom依賴

com.github.pagehelperpagehelper-spring-boot-starter1.2.3

添加配置

# pagehelper參數(shù)配置pagehelper.helperDialect=mysqlpagehelper.reasonable=truepagehelper.supportMethodsArguments=truepagehelper.returnPageInfo=checkpagehelper.params=count=countSql

代碼中使用

//PageHelper放在查詢方法前即可PageHelper.startPage(page, limit);List urList = userMapper.getUsers(userSearch);... ...//獲取分頁(yè)查詢后的pageInfo對(duì)象數(shù)據(jù)PageInfo pageInfo =newPageInfo<>(urList);//pageInfo中獲取到的總記錄數(shù)total:pageInfo.getTotal();

PageInfo對(duì)象中的數(shù)據(jù)和用法,詳見源碼!

整合ztree

詳見ztree官網(wǎng):http://www.treejs.cn/v3/api.php

整合httpClient

導(dǎo)入pom依賴

org.apache.httpcomponentshttpclient4.5.3org.apache.httpcomponentshttpmime4.5.3

配置類

/** * @項(xiàng)目名稱:wyait-manage * @包名:com.wyait.manage.config * @類描述: * @創(chuàng)建人:wyait * @創(chuàng)建時(shí)間:2018-01-11 9:13 *@version:V1.0 */@ConfigurationpublicclassHttpClientConfig{privatestaticfinalLogger logger = LoggerFactory? ? ? ? ? ? .getLogger(ShiroConfig.class);/**

? ? * 連接池最大連接數(shù)

? ? */@Value("${httpclient.config.connMaxTotal}")privateintconnMaxTotal =20;/**

? ? *

? ? */@Value("${httpclient.config.maxPerRoute}")privateintmaxPerRoute =20;/**

? ? * 連接存活時(shí)間,單位為s

? ? */@Value("${httpclient.config.timeToLive}")privateinttimeToLive =10;/**? ? * 配置連接池? ? *@return*/@Bean(name="poolingClientConnectionManager")publicPoolingHttpClientConnectionManagerpoolingClientConnectionManager(){? ? ? ? PoolingHttpClientConnectionManager poolHttpcConnManager =newPoolingHttpClientConnectionManager(60, TimeUnit.SECONDS);// 最大連接數(shù)poolHttpcConnManager.setMaxTotal(this.connMaxTotal);// 路由基數(shù)poolHttpcConnManager.setDefaultMaxPerRoute(this.maxPerRoute);returnpoolHttpcConnManager;? ? }@Value("${httpclient.config.connectTimeout}")privateintconnectTimeout =3000;@Value("${httpclient.config.connectRequestTimeout}")privateintconnectRequestTimeout =2000;@Value("${httpclient.config.socketTimeout}")privateintsocketTimeout =3000;/**? ? * 設(shè)置請(qǐng)求配置? ? *@return*/@BeanpublicRequestConfigconfig(){returnRequestConfig.custom()? ? ? ? ? ? ? ? .setConnectionRequestTimeout(this.connectRequestTimeout)? ? ? ? ? ? ? ? .setConnectTimeout(this.connectTimeout)? ? ? ? ? ? ? ? .setSocketTimeout(this.socketTimeout)? ? ? ? ? ? ? ? .build();? ? }@Value("${httpclient.config.retryTime}")// 此處建議采用@ConfigurationProperties(prefix="httpclient.config")方式,方便復(fù)用privateintretryTime;/**? ? * 重試策略? ? *@return*/@BeanpublicHttpRequestRetryHandlerhttpRequestRetryHandler(){// 請(qǐng)求重試finalintretryTime =this.retryTime;returnnewHttpRequestRetryHandler() {publicbooleanretryRequest(IOException exception,intexecutionCount, HttpContext context){// Do not retry if over max retry count,如果重試次數(shù)超過了retryTime,則不再重試請(qǐng)求if(executionCount >= retryTime) {returnfalse;? ? ? ? ? ? ? ? }// 服務(wù)端斷掉客戶端的連接異常if(exceptioninstanceofNoHttpResponseException) {returntrue;? ? ? ? ? ? ? ? }// time out 超時(shí)重試if(exceptioninstanceofInterruptedIOException) {returntrue;? ? ? ? ? ? ? ? }// Unknown hostif(exceptioninstanceofUnknownHostException) {returnfalse;? ? ? ? ? ? ? ? }// Connection refusedif(exceptioninstanceofConnectTimeoutException) {returnfalse;? ? ? ? ? ? ? ? }// SSL handshake exceptionif(exceptioninstanceofSSLException) {returnfalse;? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? HttpClientContext clientContext = HttpClientContext.adapt(context);? ? ? ? ? ? ? ? HttpRequest request = clientContext.getRequest();if(!(requestinstanceofHttpEntityEnclosingRequest)) {returntrue;? ? ? ? ? ? ? ? }returnfalse;? ? ? ? ? ? }? ? ? ? };? ? }/**? ? * 創(chuàng)建httpClientBuilder對(duì)象? ? *@paramhttpClientConnectionManager? ? *@return*/@Bean(name ="httpClientBuilder")publicHttpClientBuildergetHttpClientBuilder(@Qualifier("poolingClientConnectionManager")PoolingHttpClientConnectionManager httpClientConnectionManager){returnHttpClients.custom().setConnectionManager(httpClientConnectionManager)? ? ? ? ? ? ? ? .setRetryHandler(this.httpRequestRetryHandler())//.setKeepAliveStrategy(connectionKeepAliveStrategy())//.setRoutePlanner(defaultProxyRoutePlanner()).setDefaultRequestConfig(this.config());? ? }/**? ? * 自動(dòng)釋放連接? ? *@paramhttpClientBuilder? ? *@return*/@BeanpublicCloseableHttpClientgetCloseableHttpClient(@Qualifier("httpClientBuilder")HttpClientBuilder httpClientBuilder){returnhttpClientBuilder.build();? ? }

封裝公用類

參考項(xiàng)目源碼:HttpService HttpResult

使用

數(shù)據(jù)校驗(yàn)

本項(xiàng)目中數(shù)據(jù)校驗(yàn),前臺(tái)統(tǒng)一使用自定義的正則校驗(yàn);后臺(tái)使用兩種校驗(yàn)方式供大家選擇使用;

oval注解校驗(yàn)

//TODO?

Google或百度

自定義正則校驗(yàn)

參考:ValidateUtil.java和checkParam.js

數(shù)據(jù)庫(kù)設(shè)計(jì)

表結(jié)構(gòu)

用戶user、角色role、權(quán)限permission以及中間表(user_role、role_permission)共五張表;?

實(shí)現(xiàn)按鈕級(jí)別的權(quán)限控制。?

建表SQL源碼:github

數(shù)據(jù)源配置

單庫(kù)(數(shù)據(jù)源)配置

spring boot默認(rèn)自動(dòng)加載單庫(kù)配置,只需要在application.properties文件中添加mysql配置即可;

# mysqlspring.datasource.url=jdbc:mysql://localhost:3306/wyait?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=truespring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver# 使用druid連接池? 需要注意的是:spring.datasource.type舊的spring boot版本是不能識(shí)別的。spring.datasource.type=com.alibaba.druid.pool.DruidDataSource# mybatismybatis.type-aliases-package=com.wyait.manage.pojomybatis.mapper-locations=classpath:mapper/*.xml# 開啟駝峰映射mybatis.configuration.map-underscore-to-camel-case=true

多數(shù)據(jù)源配置

方式一:利用spring加載配置,注冊(cè)bean的邏輯進(jìn)行多數(shù)據(jù)源配置

配置文件:

# 多數(shù)據(jù)源配置slave.datasource.names=test,test1slave.datasource.test.driverClassName =com.mysql.jdbc.Driverslave.datasource.test.url=jdbc:mysql://localhost:3306/test?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=trueslave.datasource.test.username=rootslave.datasource.test.password=123456# test1slave.datasource.test1.driverClassName =com.mysql.jdbc.Driverslave.datasource.test1.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=trueslave.datasource.test1.username=rootslave.datasource.test1.password=123456

配置類

/**

* @項(xiàng)目名稱:lyd-channel

* @類名稱:MultipleDataSource

* @類描述:創(chuàng)建多數(shù)據(jù)源注冊(cè)到Spring中

* @創(chuàng)建人:wyait

* @創(chuàng)建時(shí)間:2017年12月19日 下午2:49:34

* @version:

*///@Configuration@SuppressWarnings("unchecked")publicclassMultipleDataSourceimplementsBeanDefinitionRegistryPostProcessor,EnvironmentAware{//作用域?qū)ο?private ScopeMetadataResolver scopeMetadataResolver =newAnnotationScopeMetadataResolver();//bean名稱生成器.private BeanNameGenerator beanNameGenerator =newAnnotationBeanNameGenerator();//如配置文件中未指定數(shù)據(jù)源類型,使用該默認(rèn)值privatestaticfinalObjectDATASOURCE_TYPE_DEFAULT ="com.alibaba.druid.pool.DruidDataSource";// 存放DataSource配置的集合;privateMap> dataSourceMap =newHashMap>();? ? @Override? ? publicvoidpostProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {? ? System.out.println("MultipleDataSourceBeanDefinitionRegistryPostProcessor.postProcessBeanFactory()");//設(shè)置為主數(shù)據(jù)源;beanFactory.getBeanDefinition("dataSource").setPrimary(true);if(!dataSourceMap.isEmpty()){//不為空的時(shí)候.BeanDefinition bd =null;Map dsMap =null;? ? ? ? ? ? MutablePropertyValues mpv =null;for(Entry> entry : dataSourceMap.entrySet()) {? ? ? ? ? ? ? ? bd = beanFactory.getBeanDefinition(entry.getKey());? ? ? ? ? ? ? ? mpv = bd.getPropertyValues();? ? ? ? ? ? ? ? dsMap = entry.getValue();? ? ? ? ? ? ? ? mpv.addPropertyValue("driverClassName", dsMap.get("driverClassName"));? ? ? ? ? ? ? ? mpv.addPropertyValue("url", dsMap.get("url"));? ? ? ? ? ? ? ? mpv.addPropertyValue("username", dsMap.get("username"));? ? ? ? ? ? ? ? mpv.addPropertyValue("password", dsMap.get("password"));? ? ? ? ? ? }? ? ? }? ? }? ? @Override? ? publicvoidpostProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {? ? System.out.println("MultipleDataSourceBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry()");try{if(!dataSourceMap.isEmpty()){//不為空的時(shí)候,進(jìn)行注冊(cè)bean.for(Entry> entry:dataSourceMap.entrySet()){Objecttype = entry.getValue().get("type");//獲取數(shù)據(jù)源類型if(type ==null){? ? ? ? ? ? ? ? ? ? type= DATASOURCE_TYPE_DEFAULT;? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? registerBean(registry, entry.getKey(),(Class)Class.forName(type.toString()));? ? ? ? ? ? ? }? ? ? ? ? }? ? ? }catch(ClassNotFoundException? e) {//異常捕捉.e.printStackTrace();? ? ? }? ? }/**

? ? * 注意重寫的方法 setEnvironment 是在系統(tǒng)啟動(dòng)的時(shí)候被執(zhí)行。

? ? * 這個(gè)方法主要是:加載多數(shù)據(jù)源配置

? ? * 從application.properties文件中進(jìn)行加載;

? ? */@Override? ? publicvoidsetEnvironment(Environment environment) {? ? System.out.println("MultipleDataSourceBeanDefinitionRegistryPostProcessor.setEnvironment()");/*

? ? ? ? * 獲取application.properties配置的多數(shù)據(jù)源配置,添加到map中,之后在postProcessBeanDefinitionRegistry進(jìn)行注冊(cè)。

? ? ? ? *///獲取到前綴是"slave.datasource." 的屬性列表值.RelaxedPropertyResolver propertyResolver =newRelaxedPropertyResolver(environment,"slave.datasource.");//獲取到所有數(shù)據(jù)源的名稱.StringdsPrefixs = propertyResolver.getProperty("names");String[] dsPrefixsArr = dsPrefixs.split(",");for(StringdsPrefix:dsPrefixsArr){/*

? ? ? ? ? ? * 獲取到子屬性,對(duì)應(yīng)一個(gè)map;

? ? ? ? ? ? * 也就是這個(gè)map的key就是

? ? ? ? ? ? * type、driver-class-name等;

? ? ? ? ? ? */Map dsMap = propertyResolver.getSubProperties(dsPrefix +".");//存放到一個(gè)map集合中,之后在注入進(jìn)行使用.dataSourceMap.put(dsPrefix, dsMap);? ? ? }? ? }/**

? ? * 注冊(cè)Bean到Spring

? ? */privatevoidregisterBean(BeanDefinitionRegistry registry,Stringname, Class beanClass) {? ? ? ? AnnotatedGenericBeanDefinition abd =newAnnotatedGenericBeanDefinition(beanClass);? ? ? ? ScopeMetadata scopeMetadata =this.scopeMetadataResolver.resolveScopeMetadata(abd);? ? ? ? abd.setScope(scopeMetadata.getScopeName());// 可以自動(dòng)生成nameStringbeanName = (name !=null? name :this.beanNameGenerator.generateBeanName(abd, registry));? ? ? ? AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);? ? ? ? BeanDefinitionHolder definitionHolder =newBeanDefinitionHolder(abd, beanName);? ? ? ? BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);? ? }}

接口:BeanDefinitionRegistryPostProcessor只要是注入bean,?

接口:接口 EnvironmentAware 重寫方法 setEnvironment ; 可以在工程啟動(dòng)時(shí),獲取到系統(tǒng)環(huán)境變量和application配置文件中的變量。

該配置類的加載順序是:?

setEnvironment()-->postProcessBeanDefinitionRegistry() --> postProcessBeanFactory()

在setEnvironment()方法中主要是讀取了application.properties的配置;

在postProcessBeanDefinitionRegistry()方法中主要注冊(cè)為spring的bean對(duì)象;

在postProcessBeanFactory()方法中主要是注入從setEnvironment方法中讀取的application.properties配置信息。

文章屬于轉(zhuǎn)載,原文:springboot1.5.9 + mybatis + layui + shiro后臺(tái)權(quán)限管理系統(tǒng)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,143評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,553評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,620評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,416評(píng)論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,940評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評(píng)論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,170評(píng)論 0 287
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,709評(píng)論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,597評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,784評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評(píng)論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,029評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,407評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,663評(píng)論 1 280
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,403評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,746評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,776評(píng)論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,887評(píng)論 6 342
  • 要加“m”說(shuō)明是MB,否則就是KB了. -Xms:初始值 -Xmx:最大值 -Xmn:最小值 java -Xms8...
    dadong0505閱讀 4,865評(píng)論 0 53
  • 在我搭建基于Spring Cloud的微服務(wù)體系應(yīng)用的時(shí)候所需要或者是常用的屬性配置文件,還有這些屬性的用途,此配...
    StrongManAlone閱讀 4,046評(píng)論 0 18
  • 周檢視第3周(2017年08月) 健康:1)泡腳2天 2)徒步53000步,平均7600/天 3)冥想6天 ...
    圓圓jXY閱讀 187評(píng)論 0 0