轉自https://blog.csdn.net/gaoduicai/article/details/79300464
業務場景是:登錄時根據每個人電腦MAC地址做權限校驗,因此研究了一下jeesite的登錄業務
登錄后跳操作理解
首先配置中jeesite.properties中設置:
adminPath=/a :項目管理端域名
frontPath=/f :前端域名
<form id="loginForm" class="form login-form" action="${ctx}/login" method="post">
點擊登錄會通過springmvc至后台colltrer控制器
通過控制器類可知道:
@RequestMapping(value = "${adminPath}/login", method = RequestMethod.GET)
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>