1. 程式人生 > >jeesite登入至業務詳細程式碼流轉及配置梳理

jeesite登入至業務詳細程式碼流轉及配置梳理

localhost:8080/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檔案裡;

  1. <beanid="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  2.     <propertyname="securityManager"ref="securityManager"/>
  3.     <propertyname="loginUrl"value="https://my.oschina.net/u/2601842/blog/${adminPath}/login"
    />
  4.     <propertyname="successUrl"value="https://my.oschina.net/u/2601842/blog/${adminPath}"/>
  5.     <propertyname="filters">
  6.         <map>
  7.             <entrykey="authc"value-ref="formAuthenticationFilter"/>
  8.         </map>
  9.     </property>
  10.     <propertyname="filterChainDefinitions">
  11.         <value>
  12.             /static/** = anon  
  13.             /userfiles/** = anon  
  14.             ${adminPath}/login = authc
  15.             ${adminPath}/logout = logout
  16.             ${adminPath}/** = user  
  17.         </value>
  18.     </property>
  19. </bean>
以上就是配置過程最關鍵的部分。loginuUrl屬性所指定的url表示的是所有未通過驗證的url所訪問的位置,此處就是登入介面;successUrl表示的是成功登陸的url訪問的位置,此處就是主頁。filters則是配置具體驗證方法的位置。在此處,${adminPath}/login = authc指定了/a/login,既登陸頁面,所需要的驗證許可權名為authc,又配置了authc所用的filter為formAuthenticationFilter。

        因此整個邏輯是:如果任何地方未登陸,則訪問/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下需要配置捕獲相應異常來通知使用者資訊,如果不配置異常會丟擲到頁面

  1. <bean
  2.     class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
  3.     <propertyname="exceptionMappings">
  4.         <props>
  5.             <propkey="org.apache.shiro.authz.UnauthorizedException">
  6.                 /unauthorized  
  7.             </prop>
  8.             <propkey="org.apache.shiro.authz.UnauthenticatedException">
  9.                 /unauthenticated  
  10.             </prop>
  11.         </props>
  12.     </property>
  13. </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:readwrite:aFile.txt的許可權才能執行方法someMethod()。否則丟擲異常

此處可以斷點獲取或者輸出被加密的密碼資訊.token中的