jeesite登入至業務詳細程式碼流轉及配置梳理
1.第一步訪問應用理解
其中埠號在tomcate Connector屬性設定,如果只需輸入localhost:8080 即可訪問專案需要在<Host>下設定
<Context docBase="jeesite-master" path="" reloadable="true" source="org.eclipse.jst.jee.server:jeesite-master"/></Host>其中path=""即為不許輸入工程名稱,docbase為工程路徑;
2.登入後跳操作理解
首先配置中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請求地址會被相關攔截器攔截;成功
return return "modules/sys/sysLogin";(此處則為登入介面jsp)至此訪問應用之登入jsp前端完成;
下來時登入頁面跳轉:
shiro的登陸功能在controller之前加入了一個filter。這個filter被配置在檔案spring-context-shiro.xml檔案裡;
- <beanid="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
- <propertyname="securityManager"ref="securityManager"/>
- <propertyname="loginUrl"value="https://my.oschina.net/u/2601842/blog/${adminPath}/login"
- <propertyname="successUrl"value="https://my.oschina.net/u/2601842/blog/${adminPath}"/>
- <propertyname="filters">
- <map>
- <entrykey="authc"value-ref="formAuthenticationFilter"/>
- </map>
- </property>
- <propertyname="filterChainDefinitions">
- <value>
- /static/** = anon
- /userfiles/** = anon
- ${adminPath}/login = authc
- ${adminPath}/logout = logout
- ${adminPath}/** = user
- </value>
- </property>
- </bean>
因此整個邏輯是:如果任何地方未登陸,則訪問/a/login頁面,而/a/login頁面的驗證許可權中又指定了formAuthenticationFilter做為過濾,如果過濾中驗證成功,則訪問/a這個主頁。所以,login.jsp中的表單資訊則首先交由formAuthenticationFilter首先處理。
下面為shiro許可權理解:
在JSP上的TAG實現標籤名稱 | 標籤條件(均是顯示標籤內容) |
<shiro:authenticated> | 登入之後 |
<shiro:notAuthenticated> | 不在登入狀態時 |
<shiro:guest> | 使用者在沒有RememberMe時 |
<shiro:user> | 使用者在RememberMe時 |
<shiro:hasAnyRoles name="abc,123" > | 在有abc或者123角色時 |
<shiro:hasRole name="abc"> | 擁有角色abc |
<shiro:lacksRole name="abc"> | 沒有角色abc |
<shiro:hasPermission name="abc"> | 擁有許可權資源abc |
<shiro:lacksPermission name="abc"> | 沒有abc許可權資源 |
<shiro:principal> | 預設顯示使用者名稱稱 |
後臺shiro
Shiro在註解模式下,登入失敗,與沒有許可權均是通過丟擲異常。並且預設並沒有去處理或者捕獲這些異常。在springMVC下需要配置捕獲相應異常來通知使用者資訊,如果不配置異常會丟擲到頁面
- <bean
- class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
- <propertyname="exceptionMappings">
- <props>
- <propkey="org.apache.shiro.authz.UnauthorizedException">
- /unauthorized
- </prop>
- <propkey="org.apache.shiro.authz.UnauthenticatedException">
- /unauthenticated
- </prop>
- </props>
- </property>
- </bean>
@RequiresAuthentication
驗證使用者是否登入,等同於方法subject.isAuthenticated()結果為true時。
@RequiresUser
驗證使用者是否被記憶,user有兩種含義:
一種是成功登入的(subject.isAuthenticated()結果為true);
另外一種是被記憶的(subject.isRemembered()結果為true)。
@RequiresGuest
驗證是否是一個guest的請求,與@RequiresUser完全相反。
換言之,RequiresUser== !RequiresGuest。
此時subject.getPrincipal() 結果為null.
@RequiresRoles
例如:@RequiresRoles("aRoleName");
void someMethod();
如果subject中有aRoleName角色才可以訪問方法someMethod。如果沒有這個許可權則會丟擲異常。
@RequiresPermissions
例如: @RequiresPermissions({"file:read", "write:aFile.txt"} )
void someMethod();
要求subject中必須同時含有file:read和write:aFile.txt的許可權才能執行方法someMethod()。否則丟擲異常。
此處可以斷點獲取或者輸出被加密的密碼資訊.token中的