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 进行举报,并提供相关证据,一经查实,本社区将马上删除涉嫌侵权内容。