轉自https://blog.csdn.net/gaoduicai/article/details/79300464前端
業務場景是:登陸時根據每一個人電腦MAC地址作權限校驗,所以研究了一下jeesite的登陸業務java
登陸後跳操做理解web
首先配置中jeesite.properties中設置:spring
adminPath=/a :項目管理端域名數據庫
frontPath=/f :前端域名apache
<form id="loginForm" class="form login-form" action="${ctx}/login" method="post">緩存
點擊登陸會經過springmvc至後臺colltrer控制器安全
經過控制器類可知道:session
@RequestMapping(value = "${adminPath}/login", method = RequestMethod.GET)mvc
mapping請求地址會被相關攔截器攔截;成功
<!-- 攔截器配置,攔截順序:先執行後定義的,排在第一位的最後執行。-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="${adminPath}/**" />
<mvc:mapping path="/rest/**" />
<mvc:exclude-mapping path="${adminPath}/"/>
<mvc:exclude-mapping path="${adminPath}/login"/>
<mvc:exclude-mapping path="${adminPath}/sys/menu/tree"/>
<mvc:exclude-mapping path="${adminPath}/sys/menu/treeData"/>
<mvc:exclude-mapping path="${adminPath}/oa/oaNotify/self/count"/>
<bean class="com.thinkgem.jeesite.modules.sys.interceptor.LogInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
return return "modules/sys/sysLogin";(此處則爲登陸界面jsp)至此訪問應用之登陸jsp前端完成;
下來時登陸頁面跳轉:
shiro的登錄功能在controller以前加入了一個filter。這個filter被配置在文件spring-context-shiro.xml文件裏;
<!-- Shiro權限過濾過濾器定義 --> <bean name="shiroFilterChainDefinitions" class="java.lang.String"><!-- 配置shiro要攔截的請求 --> <constructor-arg> <value> /static/** = anon /userfiles/** = anon ${adminPath}/cas = cas ${adminPath}/login = authc ${adminPath}/logout = logout ${adminPath}/** = authc /act/editor/** = user /ReportServer/** = user /rest/** = anon /chat/** = authc </value> </constructor-arg> </bean> <!-- 安全認證過濾器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /><!-- <property name="loginUrl" value="${cas.server.url}?service=${cas.project.url}${adminPath}/cas" /> --> <property name="loginUrl" value="${adminPath}/login" /> <property name="successUrl" value="${adminPath}?login" /><!-- 登陸成功跳轉頁面 --> <property name="filters"> <map> <entry key="cas" value-ref="casFilter"/> <entry key="authc" value-ref="formAuthenticationFilter"/> <entry key="logout" value-ref="systemLogoutFilter" /> </map> </property> <property name="filterChainDefinitions"> <ref bean="shiroFilterChainDefinitions"/> </property> </bean>
以上就是配置過程最關鍵的部分。loginUrl屬性所指定的url表示的是全部未經過驗證的url所訪問的位置,此處就是登陸界面;
successUrl表示的是成功登錄的url訪問的位置,此處就是主頁。filters則是配置具體驗證方法的位置。
在此處,${adminPath}/login = authc指定了/a/login,既登錄頁面,所須要的驗證權限名爲authc,又配置了authc所用的
filter爲formAuthenticationFilter。
所以整個邏輯是:若是任何地方未登錄,則訪問/a/login頁面,而/a/login頁面的驗證權限中又指定了formAuthenticationFilter
作爲過濾,若是過濾中驗證成功,則訪問/a這個主頁。因此,login.jsp中的表單信息則首先交由formAuthenticationFilter首先處理。
formAuthenticationFilter中建立createToken ,在這裏建立token的時候屬性需添加mac地址
<!-- 定義Shiro安全管理配置 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="systemAuthorizingRealm" /><!--讀取數據文件--> <property name="sessionManager" ref="sessionManager" /><!-- 管理會話信息 --> <property name="cacheManager" ref="shiroCacheManager" /><!--管理shiro緩存 --> </bean>
在systemAuthorizingRealm進行數據安全權限校驗,在這裏能夠經過token中mac地址和數據庫中數據進行比對,若是不匹配,返回驗證信息
後臺shiro
Shiro在註解模式下,登陸失敗,與沒有權限均是經過拋出異常。而且默認並無去處理或者捕獲這些異常。在springMVC下須要配置捕獲
相應異常來通知用戶信息,若是不配置異常會拋出到頁面
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.apache.shiro.authz.UnauthorizedException">error/403</prop> <prop key="java.lang.Throwable">error/500</prop> </props> </property> </bean>