JAVA CAS單點登陸之二:CAS普通模式1演練

前言css

通過上一JAVA CAS單點登陸之一:搭建CAS服務器 這一章,CAS服務器已經搭建好了。固然這時候的CAS服務器僅僅是最第一版本的。好比密碼驗證,頁面美觀度都須要進一步調整。但這都是可有可無的。html

最主要的是先把整個一套認證流程走下來,至於完善的工做,都是沒個點的工做了,相對比較簡單。java

主要內容web

   1. 新建一個web應用mywebapp1,測試與CAS服務器的認證效果瀏覽器

    2.若是上一步驟1認證成功的話,將mywebapp1複製一份,調整若干參數,繼續測試。緩存

    若是步驟1,步驟2都認證成功的話,則表示環境搭建成功tomcat

具體參數  服務器

涉及的全部參數都在個人實體機(WIN7)完成的。分別按照了3個TOMCAT服務端。session

  • Tomcat6.0.36app

  • JDK7

  • CAS Server版本:cas-server-3.5.3

  • CAS Client版本:cas-client-3.1.1

 

域名映射(C:\Windows\System32\drivers\etc\hosts)

1

2

127.0.0.1 hellocas1.com

127.0.0.1 hellocas2.com

主機名

zhaoguoyu-pc

 

重要概念及認證流程介紹

我就不重複貼了,參考一下連接

http://www.coin163.com/java/cas/ticket.html

http://www.cnblogs.com/vhua/p/cas_6.html

http://steven-wiki.readthedocs.org/en/latest/security/

http://www.blogjava.net/security/archive/2006/10/02/sso_in_action.html

 

操做步驟

1.cas server端在CAS普通模式時,不須要特殊配置(記住端口是8888,443)

2.部署第一個Cas Client app 。應用命名爲mywebapp1。

    我使用的是Maven方式部署的。

    2.1 使用archetype-webapp插件建立一個項目(略)

    2.2修改pom.xml文件,添加依賴

1

2

3

4

5

    <dependency>

      <groupId>org.jasig.cas</groupId>

      <version>3.1.1</version>

      <artifactId>cas-client-core</artifactId>

    </dependency>

    2.3配置web.xml

    

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="mywebapp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 

  <display-name>mywebapp</display-name>

 

  <description>

 

    Simple sample, how to use CAS Java Client 3.x.

    In this sample exists a public area (/)

    and a private area (/protected/*).

 

  </description>

  <!-- 用於單點退出,該過濾器用於實現單點登出功能,可選配置-->

  <!-- Sign out not yet implemented -->

  <!--

      <filter>

          <filter-name>CAS Single Sign Out Filter</filter-name>

          <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>

      </filter>

  -->

  <!-- 該過濾器負責用戶的認證工做,必須啓用它 -->

  <filter>

    <filter-name>CAS Authentication Filter</filter-name>

    <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>

    <init-param>

      <param-name>casServerLoginUrl</param-name>

      <param-value>https://zhaoguoyu-pc/cas/login</param-value>

    </init-param>

    <init-param>

      <param-name>serverName</param-name>

      <param-value>http://zhaoguoyu-pc:8080</param-value>

    </init-param>

    <init-param>

      <param-name>renew</param-name>

      <param-value>false</param-value>

    </init-param>

    <init-param>

      <param-name>gateway</param-name>

      <param-value>false</param-value>

    </init-param>

  </filter>

  <!-- 該過濾器負責對Ticket的校驗工做,必須啓用它 -->

  <filter>

    <filter-name>CAS Validation Filter</filter-name>

    <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>

    <init-param>

      <param-name>casServerUrlPrefix</param-name>

      <param-value>https://zhaoguoyu-pc/cas/</param-value>

    </init-param>

    <init-param>

      <param-name>serverName</param-name>

      <param-value>http://zhaoguoyu-pc:8080</param-value>

    </init-param>

 

  </filter>

  <!--

      該過濾器負責實現HttpServletRequest請求的包裹,

      好比容許開發者經過HttpServletRequest的getRemoteUser()方法得到SSO登陸用戶的登陸名,可選配置。

  -->

  <filter>

    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>

    <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>

  </filter>

  <!--

      該過濾器使得開發者能夠經過org.jasig.cas.client.util.AssertionHolder來獲取用戶的登陸名。

      好比AssertionHolder.getAssertion().getPrincipal().getName()。

  -->

  <filter>

    <filter-name>CAS Assertion Thread Local Filter</filter-name>

    <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>

  </filter>

 

  <!-- ************************* -->

 

  <!-- Sign out not yet implemented -->

  <!--

      <filter-mapping>

          <filter-name>CAS Single Sign Out Filter</filter-name>

          <url-pattern>/*</url-pattern>

      </filter-mapping>

  -->

 

  <filter-mapping>

    <filter-name>CAS Authentication Filter</filter-name>

    <url-pattern>/protected/*</url-pattern>

  </filter-mapping>

 

  <filter-mapping>

    <filter-name>CAS Validation Filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

 

  <filter-mapping>

    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

 

  <filter-mapping>

    <filter-name>CAS Assertion Thread Local Filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

 

  <!--  *********************** -->

 

  <!-- Sign out not yet implemented -->

  <!--

      <listener>

          <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>

      </listener>

  -->

 

  <!--  *********************** -->

 

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

</web-app>

須要注意的是serverName參數,很少解釋。

3.3 修改HttpServletRequestWrapperFilter類,修復client3.1.1存在的BUG。

詳情見https://issues.jasig.org/browse/CASC-50

1

2

3

4

5

6

7

8

9

10

11

12

13

public void doFilter(final ServletRequest servletRequest,

                     final ServletResponse servletResponse, final FilterChain filterChain)

        throws IOException, ServletException {

    final Principal principal = retrievePrincipalFromSessionOrRequest(servletRequest);

 

    //   filterChain.doFilter(new CasHttpServletRequestWrapper(

    //         (HttpServletRequest) servletRequest, principal), servletResponse);

    if (principal != null) {

        filterChain.doFilter(new CasHttpServletRequestWrapper((HttpServletRequest) servletRequest, principal), servletResponse);

    else {

        filterChain.doFilter(servletRequest, servletResponse);

    }

}

  

 3.4 準備測試JSP,將他放到protected目錄下, 由於要和web.xml中/protected/*匹配對應。

1

2

3

4

<dl>

   <dt>Your user name:</dt>

   <dd><%= request.getRemoteUser()== null ? "null" : request.getRemoteUser() %></dd>

</dl>

這時候,若是你把request對象打印出來,已是被CAS包裝的請求對象了。

或者這樣測

1

2

3

4

5

6

7

8

9

10

11

<%@page import="org.jasig.cas.client.util.AbstractCasFilter"%>

<%@page import="org.jasig.cas.client.validation.Assertion"%>

<%@page import="org.jasig.cas.client.util.AssertionHolder"%>

<%@page import="java.util.Iterator"%>

<%@page import="java.util.Map"%>

<%

   Assertion assertion1 = (Assertion) session.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);

%>

<dl>

<dd><%= assertion1.getPrincipal().getName() %></dd

</dl>

3.5文件效果

wKioL1buWuKCYUEjAABUmzEWA3Y345.png

其中未提到的include_*.jsp,*.css,能夠無視。

3.6驗證mywebapp1

3.6驗證mywebapp1

  3.6.1 瀏覽器訪問http://zhaoguoyu-pc:8080/app01/protected/,直接跳轉

到https://zhaoguoyu-pc/cas/login?service=http%3A%2F%2Fzhaoguoyu-pc%3A8080%2Fapp01%2Fprotected%2F.

發現後綴有了service

wKioL1buYrTDud5ZAACOLOGqia8787.png

spacer.gif

3.6.2 輸入用戶名和密碼(test/test),進行認證,跳轉到

http://zhaoguoyu-pc:8080/app01/protected/?ticket=ST-7-x6Txage1j1plr45vhHeN-cas01.example.org

wKioL1buYtiydIScAAAcKf7bY58855.png

發現後綴有了ticket. 成功實現了跳轉。mywebapp1驗證經過

 

spacer.gif3.7複製mywebapp1 爲mywebapp2

3.8編輯mywebapp2的web.xml文件, 僅僅修改下serviceName便可,注意端口和應用名,不然會出現找不到頁面問題。

3.9.強最新編輯的mywebapp2,複製到另一個tomcat的webapp目錄下

3.10 重複3.6 的步驟驗證mywebapp2

3.11 集成測試

  (0)清理瀏覽器緩存後

(1)先訪問mywebapp01,進行身份認證。

(2)認證後,再訪問mywebapp2,看是否須要再認證。若是不須要重複認證則表示演練完成。

 

最後,總體來講,配置模式1仍是比較簡單。可是很不幸,我上週本身演練時,碰巧遇到CAS的BUG(上面3.3步驟中修復的就是它),花費了我2個晚上。我不太清楚,爲何這個BUG就沒人在文檔中提到呢。

 

接下來的演練時CAS代理模式,我又遇到一個更坑爹的問題,一樣在國內博客中也是沒有遇到。WHY,爲何老天對我如此不公啊。

 

轉載:http://dba10g.blog.51cto.com/764602/1753151

版權聲明:本文內容由互聯網用戶自發貢獻,版權歸做者全部,本社區不擁有全部權,也不承擔相關法律責任。若是您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至:yqgroup@service.aliyun.com 進行舉報,並提供相關證據,一經查實,本社區將馬上刪除涉嫌侵權內容。

相關文章
相關標籤/搜索