单点登录 梦里梦外; 2022-05-16 08:04 284阅读 0赞 ## **1.3 CAS服务端部署** ## Cas服务端其实就是一个war包。 在资源\\cas\\source\\cas-server-4.0.0-release\\cas-server-4.0.0\\modules目录下 cas-server-webapp-4.0.0.war 将其改名为cas.war放入tomcat目录下的webapps下。启动tomcat自动解压war包。浏览器输入http://localhost:8080/cas/login ,可看到登录页面 不要嫌弃这个页面丑,我们后期可以再提升它的颜值。暂时把注意力放在功能实现上。 这里有个固定的用户名和密码 casuser /Mellon 登录成功后会跳到登录成功的提示页面 ## **1.4 CAS服务端配置** ## ### **1.4.1端口修改** ### 如果我们不希望用8080端口访问CAS, 可以修改端口 1. 修改TOMCAT的端口 打开tomcat 目录 conf\\server.xml 找到下面的配置 将端口8080,改为9100 1. 修改CAS配置文件 修改cas的WEB-INF/cas.properties <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">server.name=http://localhost:</span><span style="color:#ff0000;">9</span><span style="color:#ff0000;">1</span><span style="color:#ff0000;">00</span></span></p> </td> </tr> </tbody> </table> ### **1.4.2去除https认证** ### CAS默认使用的是HTTPS协议,如果使用HTTPS协议需要SSL安全证书(需向特定的机构申请和购买) 。如果对安全要求不高或是在开发测试阶段,可使用HTTP协议。我们这里讲解通过修改配置,让CAS使用HTTP协议。 1. 修改cas的WEB-INF/deployerConfigContext.xml 找到下面的配置 <table style="width:426.1pt;"> <tbody> <tr> <td style="background-color:#f1f1f1;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"</span><br><span style="color:#1e1c11;">p:httpClient-ref="httpClient"/></span></span></p> </td> </tr> </tbody> </table> 这里需要增加参数p:requireSecure="false",requireSecure属性意思为是否需要安全验证,即HTTPS,false为不采用 1. 修改cas的/WEB-INF/[**spring**][spring]\-configuration/ticketGrantingTicketCookieGenerator.xml 找到下面配置 <table style="width:426.1pt;"> <tbody> <tr> <td style="background-color:#f1f1f1;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:cookieSecure="true"</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:cookieMaxAge="-1"</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:cookieName="CASTGC"</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:cookiePath="/cas" /></span></span></p> </td> </tr> </tbody> </table> 参数p:cookieSecure="true",同理为HTTPS验证相关,TRUE为采用HTTPS验证,FALSE为不采用https验证。 参数p:cookieMaxAge="-1",是COOKIE的最大生命周期,-1为无生命周期,即只在当前打开的窗口有效,关闭或重新打开其它窗口,仍会要求验证。可以根据需要修改为大于0的数字,比如3600等,意思是在3600秒内,打开任意窗口,都不需要验证。 我们这里将cookieSecure改为false , cookieMaxAge 改为3600 (3)修改cas的WEB-INF/spring\-configuration/warnCookieGenerator.xml 找到下面配置 <table style="width:426.1pt;"> <tbody> <tr> <td style="background-color:#f1f1f1;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"</span><br><span style="color:#1e1c11;">p:cookieSecure="true"</span><br><span style="color:#1e1c11;">p:cookieMaxAge="-1"</span><br><span style="color:#1e1c11;">p:cookieName="CASPRIVACY"</span><br><span style="color:#1e1c11;">p:cookiePath="/cas" /></span></span></p> </td> </tr> </tbody> </table> 我们这里将cookieSecure改为false , cookieMaxAge 改为3600 ## **1.5 CAS客户端入门小Demo** ## ### **1.5.1客户端工程1搭建** ### (1)搭建工程引入依赖 创建Maven工程 (war)casclient\_demo1 引入cas客户端依赖并制定tomcat运行端口为9001 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">dependencies</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- </span><u><span style="color:#3f5fbf;"><u>cas</u></span></u><span style="color:#3f5fbf;"> --></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">dependency</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span><span style="color:#000000;">org.jasig.cas.client</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span><u><span style="color:#000000;"><u>cas</u></span></u><span style="color:#000000;">-client-core</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span><span style="color:#000000;">3.</span><span style="color:#000000;">3</span><span style="color:#000000;">.</span><span style="color:#000000;">3</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">dependency</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">dependency</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span><span style="color:#000000;">javax.servlet</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span><u><span style="color:#000000;"><u>servlet</u></span></u><span style="color:#000000;">-</span><u><span style="color:#000000;"><u>api</u></span></u><span style="color:#008080;"></</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span><span style="color:#000000;">2.5</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">scope</span><span style="color:#008080;">></span><span style="color:#000000;">provided</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">scope</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">dependency</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">dependencies</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">build</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">plugins</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">plugin</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span><span style="color:#000000;">org.apache.maven.plugins</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span><u><span style="color:#000000;"><u>maven</u></span></u><span style="color:#000000;">-compiler-</span><u><span style="color:#000000;"><u>plugin</u></span></u><span style="color:#008080;"></</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span><span style="color:#000000;">2.3.2</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">configuration</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">source</span><span style="color:#008080;">></span><span style="color:#000000;">1.7</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">source</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">target</span><span style="color:#008080;">></span><span style="color:#000000;">1.7</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">target</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">configuration</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">plugin</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">plugin</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span><span style="color:#000000;">org.apache.tomcat.maven</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span><span style="color:#000000;">tomcat7-</span><u><span style="color:#000000;"><u>maven</u></span></u><span style="color:#000000;">-</span><u><span style="color:#000000;"><u>plugin</u></span></u><span style="color:#008080;"></</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">configuration</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- 指定端口 --></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">port</span><span style="color:#008080;">></span><span style="color:#000000;">9</span><span style="color:#000000;">0</span><span style="color:#000000;">01</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">port</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- 请求路径 --></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">path</span><span style="color:#008080;">></span><span style="color:#000000;">/</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">path</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">configuration</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">plugin</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">plugins</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">build</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> (2)添加web.xml <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><?</span><span style="color:#3f7f7f;">xml</span> <span style="color:#7f007f;">version</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"1.0"</em></span></em> <span style="color:#7f007f;">encoding</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"UTF-8"</em></span></em><span style="color:#008080;">?></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">web-app</span> <span style="color:#7f007f;">xmlns:xsi</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://www.w3.org/2001/XMLSchema-instance"</em></span></em></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f007f;">xmlns</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://java.sun.com/xml/ns/javaee"</em></span></em></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f007f;">xsi:schemaLocation</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</em></span></em></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f007f;">version</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"2.5"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">listener</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">listener-class</span><span style="color:#008080;">></span><span style="color:#000000;">org.jasig.cas.client.session.SingleSignOutHttpSessionListener</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">listener-class</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">listener</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 该过滤器用于实现单点登出功能,可选配置。 --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CAS Single Sign Out Filter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span><span style="color:#000000;">org.jasig.cas.client.session.SingleSignOutFilter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CAS Single Sign Out Filter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span><span style="color:#000000;">/*</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 该过滤器负责用户的认证工作,必须启用它 --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CASFilter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span><span style="color:#000000;">org.jasig.cas.client.authentication.AuthenticationFilter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span><span style="color:#000000;">casServerLoginUrl</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span><span style="color:#000000;">http://localhost:9</span><span style="color:#000000;">1</span><span style="color:#000000;">00/cas/login</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!--这里的server是服务端的IP --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span><span style="color:#000000;">serverName</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span><span style="color:#000000;">http://localhost:9</span><span style="color:#000000;">0</span><span style="color:#000000;">01</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CASFilter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span><span style="color:#000000;">/*</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 该过滤器负责对Ticket的校验工作,必须启用它 --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CAS Validation Filter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span><span style="color:#000000;"> org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span><span style="color:#000000;">casServerUrlPrefix</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span><span style="color:#000000;">http://localhost:9</span><span style="color:#000000;">1</span><span style="color:#000000;">00/cas</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span><span style="color:#000000;">serverName</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span><span style="color:#000000;">http://localhost:9001</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CAS Validation Filter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span><span style="color:#000000;">/*</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 该过滤器负责实现HttpServletRequest请求的包裹, 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。 --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CAS HttpServletRequest Wrapper Filter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;"> org.jasig.cas.client.util.HttpServletRequestWrapperFilter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CAS HttpServletRequest Wrapper Filter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span><span style="color:#000000;">/*</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。 比如AssertionHolder.getAssertion().getPrincipal().getName()。 --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CAS Assertion Thread Local Filter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span><span style="color:#000000;">org.jasig.cas.client.util.AssertionThreadLocalFilter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-class</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span><span style="color:#000000;">CAS Assertion Thread Local Filter</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-name</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span><span style="color:#000000;">/*</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">filter-mapping</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">web-app</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> (3)编写index.jsp <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#bf5f3f;"><%@</span> <span style="color:#3f7f7f;">page</span> <span style="color:#7f007f;">language</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"java"</em></span></em> <span style="color:#7f007f;">contentType</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"text/html; charset=utf-8"</em></span></em></span></p> <p><span style="color:#1e1c11;"> <span style="color:#7f007f;">pageEncoding</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"utf-8"</em></span></em><span style="color:#bf5f3f;">%></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><!</span><span style="color:#3f7f7f;">DOCTYPE</span> <span style="color:#008080;">html</span> <span style="color:#808080;">PUBLIC</span> <span style="color:#008080;">"-//W3C//DTD HTML 4.01 Transitional//EN"</span> <span style="color:#3f7f5f;">"http://www.w3.org/TR/html4/loose.dtd"</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">html</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">head</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">meta</span> <span style="color:#7f007f;">http-equiv</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"Content-Type"</em></span></em> <span style="color:#7f007f;">content</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"text/html; charset=utf-8"</em></span></em><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">title</span><span style="color:#008080;">></span><span style="color:#000000;">一品优购</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">title</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">head</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">body</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">欢迎来到一品优购</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#bf5f3f;"><%=</span><span style="color:#000000;">request.getRemoteUser()</span><span style="color:#bf5f3f;">%></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">body</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">html</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> request.getRemoteUser()为获取远程登录名 ### **1.5.2客户端工程2搭建** ### (1)创建Maven工程 (war)casclient\_demo2 引入cas客户端依赖并制定tomcat运行端口为9002 (2)创建web.xml,参照casclient\_demo1 ,将serverName的值改为http://localhost:9002,一共两处 (3)创建index.jsp ,内容显示“欢迎来到二品优购” ### **1.5.3单点登录测试** ### 1. 启动cas部署的tomcat 2. 启动客户端工程1和客户端工程2 3. 地址栏输入http://localhost:9001/ 和http://localhost:9002/ ,地址均会跳转到CAS登录页 4. 输入用户名和密码后,页面跳转回9002 ,再次访问9001也可以打开主页面。 ### **1.5.4单点退出登录** ### 地址栏输入 [http://localhost:9100/cas/logout][http_localhost_9100_cas_logout] 即可看到退出后的提示页面 我们可以将这个链接添加到index.jsp中 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">a</span> <span style="color:#7f007f;">href</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://localhost:9</em></span></em><em><span style="color:#2a00ff;"><em>1</em></span></em><em><span style="color:#2a00ff;"><em>00/cas/logout"</em></span></em><span style="color:#008080;">></span><span style="color:#000000;">退出登录</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">a</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> 但我们更希望退出登录后,能自动跳转到某个页面,那如何处理呢? 修改cas系统的配置文件cas-servlet.xml <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <bean id="logoutAction" class="org.jasig.cas.web.flow.LogoutAction"</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:servicesManager-ref="servicesManager"</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:followServiceRedirects="${cas.logout.followServiceRedirects:</span><span style="color:#ff0000;">true</span><span style="color:#1e1c11;">}"/></span></span></p> </td> </tr> </tbody> </table> 改为true后,可以在退出时跳转页面到目标页面,修改index.jsp的退出链接 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">a</span> <span style="color:#7f007f;">href</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://localhost:9</em></span></em><em><span style="color:#2a00ff;"><em>1</em></span></em><em><span style="color:#2a00ff;"><em>00/cas/logout?service=http://</em></span></em><em><span style="color:#2a00ff;"><em>www.baidu.com</em></span></em><em><span style="color:#2a00ff;"><em>"</em></span></em><span style="color:#008080;">></span><span style="color:#000000;">退出登录</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">a</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> # **2.CAS服务端数据源设置** # ## **2.1需求分析** ## 我们现在让用户名密码从我们的品优购的user表里做验证 ## **2.2配置数据源** ## (1)修改cas服务端中web-inf下deployerConfigContext.xml ,添加如下配置 <table style="width:426.1pt;"> <tbody> <tr> <td style="background-color:#f1f1f1;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:driverClass="com.mysql.jdbc.Driver" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/pinyougoudb?characterEncoding=utf8" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:user="root" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:password="123456" /> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><bean id="passwordEncoder" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">c:encodingAlgorithm="MD5" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">p:characterEncoding="UTF-8" /> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><bean id="dbAuthHandler" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:dataSource-ref="dataSource" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:sql="select password from tb_user where username = ?" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> p:passwordEncoder-ref="passwordEncoder"/> </span></span></p> </td> </tr> </tbody> </table> 然后在配置文件开始部分找到如下配置 <table style="width:426.1pt;"> <tbody> <tr> <td style="background-color:#f1f1f1;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager"></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <constructor-arg></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <map> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" /></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" /></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> </map></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> </constructor-arg> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <property name="authenticationPolicy"></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" /></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> </property></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"></bean></span></span></p> </td> </tr> </tbody> </table> 其中 <table style="width:426.1pt;"> <tbody> <tr> <td style="background-color:#f1f1f1;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" /></span></span></p> </td> </tr> </tbody> </table> 一句是使用固定的用户名和密码,我们在下面可以看到这两个bean ,如果我们使用数据库认证用户名和密码,需要将这句注释掉。 添加下面这一句配置 <table style="width:426.1pt;"> <tbody> <tr> <td style="background-color:#f1f1f1;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver"/></span></span></p> </td> </tr> </tbody> </table> (2)将以下三个jar包放入webapps\\cas\\WEB-INF\\lib下 (这三个jar包在资源\\cas\\jar目录下) 用数据库中的用户名和密码进行测试 # **3.CAS服务端界面改造** # ## **3.1需求分析** ## 我们现在动手将CAS默认的登录页更改为自己的品优购登陆页 ## **3.2改头换面** ## ### **3.2.1拷贝资源** ### (1)将品优购的登陆页login.html拷贝到cas系统下WEB-INF\\view\\jsp\\default\\ui 目录下 (2)将css js等文件夹拷贝到 cas目录下 (3) 将原来的casLoginView.jsp 改名(可以为之后的修改操作做参照),将login.html改名为casLoginView.jsp ### **3.2.2修改页面** ### **编辑casLoginView.jsp 内容** (1)添加指令 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><%@ page pageEncoding="UTF-8" %></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><%@ page contentType="text/html; charset=UTF-8" %></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %></span></span></p> </td> </tr> </tbody> </table> (2)修改form标签 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><form:form method="post" id="fm1" commandName="${commandName}" htmlEscape="true" class="sui-form"></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">......</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"></form:form></span></span></p> </td> </tr> </tbody> </table> 1. 修改用户名框 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><form:input id="username" tabindex="1" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">accesskey="${userNameAccessKey}" path="username" autocomplete="off" htmlEscape="true" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">placeholder="邮箱/用户名/手机号" class="span2 input-xfat" /></span></span></p> </td> </tr> </tbody> </table> 1. 修改密码框 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> <form:password id="password" tabindex="2" path="password" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> accesskey="${passwordAccessKey}" htmlEscape="true" autocomplete="off" </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"> placeholder="请输入密码" class="span2 input-xfat" /></span></span></p> </td> </tr> </tbody> </table> 1. 修改登陆按钮 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><input type="hidden" name="lt" value="${loginTicket}" /></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><input type="hidden" name="execution" value="${flowExecutionKey}" /></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><input type="hidden" name="_eventId" value="submit" /></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><input class="sui-btn btn-block btn-xlarge btn-danger" accesskey="l" value="登陆" type="submit" /></span></span></p> </td> </tr> </tbody> </table> 修改后效果如下: ## **3.3错误提示** ## 在表单内加入错误提示框 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><form:errors path="*" id="msg" cssClass="errors" element="div" htmlEscape="false" /></span></span></p> </td> </tr> </tbody> </table> 测试:输入错误的用户名和密码,提示是英文。这个提示信息是在WEB-INF\\classes目录下的messages.properties文件中 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">authenticationFailure.AccountNotFoundException=Invalid credentials.</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">authenticationFailure.FailedLoginException=Invalid credentials.</span></span></p> </td> </tr> </tbody> </table> 设置国际化为zn\_CN ,修改cas-servlet.xml <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;"><bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" p:defaultLocale="</span><span style="color:#ff0000;">zh_CN</span><span style="color:#1e1c11;">" /></span></span></p> </td> </tr> </tbody> </table> 我们需要将此信息拷贝到messages\_zh\_CN.properties下,并改为中文提示(转码) <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">authenticationFailure.AccountNotFoundException=\u7528\u6237\u4E0D\u5B58\u5728.</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">authenticationFailure.FailedLoginException=\u5BC6\u7801\u9519\u8BEF.</span></span></p> </td> </tr> </tbody> </table> 第一个是用户名不存在时的错误提示 第二个是密码错误的提示 # **4. CAS客户端与SpringSecurity集成** # ## **4.1 Spring Security测试工程搭建** ## (1)建立Maven项目casclient\_demo3 ,引入spring依赖和spring secrity 相关依赖 ,tomcat端口设置为9003 (2)建立web.xml ,添加过滤器等配置 (3)创建配置文件spring-security.xml (4)添加html页面 以上步骤参照我们第4章的spring-security-demo ## **4.2 Spring Security与 CAS集成** ## (1)引入依赖 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">dependency</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span><span style="color:#000000;">org.springframework.security</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span><span style="color:#000000;">spring-security-</span><u><span style="color:#000000;"><u>cas</u></span></u><span style="color:#008080;"></</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span><span style="color:#000000;">4.1.0.RELEASE</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">dependency</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">dependency</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span><span style="color:#000000;">org.jasig.cas.client</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span><u><span style="color:#000000;"><u>cas</u></span></u><span style="color:#000000;">-client-core</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span><span style="color:#000000;">3.3.3</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">version</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">exclusions</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">exclusion</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span><span style="color:#000000;">org.slf4j</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">groupId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span><span style="color:#000000;">log4j-over-slf4j</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">artifactId</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">exclusion</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">exclusions</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">dependency</span><span style="color:#008080;">></span> </span></p> </td> </tr> </tbody> </table> (2)修改spring-security.xml <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><?</span><span style="color:#3f7f7f;">xml</span> <span style="color:#7f007f;">version</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"1.0"</em></span></em> <span style="color:#7f007f;">encoding</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"UTF-8"</em></span></em><span style="color:#008080;">?></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:beans</span> <span style="color:#7f007f;">xmlns</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://www.springframework.org/schema/security"</em></span></em></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f007f;">xmlns:beans</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://www.springframework.org/schema/beans"</em></span></em> <span style="color:#7f007f;">xmlns:xsi</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://www.w3.org/2001/XMLSchema-instance"</em></span></em></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f007f;">xsi:schemaLocation</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</em></span></em></span></p> <p><span style="color:#1e1c11;"><em><span style="color:#2a00ff;"><em>http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"</em></span></em><span style="color:#008080;">></span></span></p> <p> </p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- entry-point-</span><u><span style="color:#3f5fbf;"><u>ref</u></span></u><span style="color:#3f5fbf;"> 入口点引用 --></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">use-expressions</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"false"</em></span></em> <span style="color:#7f007f;">entry-point-ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"casProcessingFilterEntryPoint"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">intercept-url</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/**"</em></span></em> <span style="color:#7f007f;">access</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"ROLE_USER"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">csrf</span> <span style="color:#7f007f;">disabled</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"true"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- custom-filter为过滤器, position 表示将过滤器放在指定的位置上,before表示放在指定位置之前 ,after表示放在指定的位置之后 --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">custom-filter</span> <span style="color:#7f007f;">ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"casAuthenticationFilter"</em></span></em> <span style="color:#7f007f;">position</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"CAS_FILTER"</em></span></em> <span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">custom-filter</span> <span style="color:#7f007f;">ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"requestSingleLogoutFilter"</em></span></em> <span style="color:#7f007f;">before</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"LOGOUT_FILTER"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">custom-filter</span> <span style="color:#7f007f;">ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"singleLogoutFilter"</em></span></em> <span style="color:#7f007f;">before</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"CAS_FILTER"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- CAS入口点 开始 --></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">id</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"casProcessingFilterEntryPoint"</em></span></em> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.springframework.security.cas.web.CasAuthenticationEntryPoint"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 单点登录服务器登录URL --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"loginUrl"</em></span></em> <span style="color:#7f007f;">value</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://localhost:9100/cas/login"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"serviceProperties"</em></span></em> <span style="color:#7f007f;">ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"serviceProperties"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:bean</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">id</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"serviceProperties"</em></span></em> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.springframework.security.cas.ServiceProperties"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!--service 配置自身工程的根地址+/login/</span><u><span style="color:#3f5fbf;"><u>cas</u></span></u><span style="color:#3f5fbf;"> --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"service"</em></span></em> <span style="color:#7f007f;">value</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://localhost:9003/login/cas"</em></span></em><span style="color:#008080;">/></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:bean</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- CAS入口点 结束 --></span></span></p> <p><span style="color:#1e1c11;"> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 认证过滤器 开始 --></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">id</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"casAuthenticationFilter"</em></span></em> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.springframework.security.cas.web.CasAuthenticationFilter"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"authenticationManager"</em></span></em> <span style="color:#7f007f;">ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"authenticationManager"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:bean</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- 认证管理器 --></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">authentication-manager</span> <span style="color:#7f007f;">alias</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"authenticationManager"</em></span></em><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">authentication-provider</span> <span style="color:#7f007f;">ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"casAuthenticationProvider"</em></span></em><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">authentication-provider</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">authentication-manager</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- 认证提供者 --></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">id</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"casAuthenticationProvider"</em></span></em> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.springframework.security.cas.authentication.CasAuthenticationProvider"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"authenticationUserDetailsService"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:constructor-arg</span> <span style="color:#7f007f;">ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"userDetailsService"</em></span></em> <span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:bean</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:property</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"serviceProperties"</em></span></em> <span style="color:#7f007f;">ref</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"serviceProperties"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- ticketValidator 为票据验证器 --></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"ticketValidator"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.jasig.cas.client.validation.Cas20ServiceTicketValidator"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:constructor-arg</span> <span style="color:#7f007f;">index</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"0"</em></span></em> <span style="color:#7f007f;">value</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://localhost:9100/cas"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:bean</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:property</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"key"</em></span></em> <span style="color:#7f007f;">value</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"an_id_for_this_auth_provider_only"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:bean</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 认证类 --></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">id</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"userDetailsService"</em></span></em> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"cn.itcast.demo.service.UserDetailServiceImpl"</em></span></em><span style="color:#008080;">/></span> </span></p> <p> </p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- 认证过滤器 结束 --></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- 单点登出 开始 --></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">id</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"singleLogoutFilter"</em></span></em> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.jasig.cas.client.session.SingleSignOutFilter"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">id</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"requestSingleLogoutFilter"</em></span></em> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.springframework.security.web.authentication.logout.LogoutFilter"</em></span></em><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:constructor-arg</span> <span style="color:#7f007f;">value</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"http://localhost:9100/cas/logout?service=http://www.baidu.com"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:constructor-arg</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:bean</span> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:constructor-arg</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">beans:property</span> <span style="color:#7f007f;">name</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"filterProcessesUrl"</em></span></em> <span style="color:#7f007f;">value</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/logout/cas"</em></span></em><span style="color:#008080;">/></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:bean</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 单点登出 结束 --></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">beans:beans</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> (3)创建UserDetailsServiceImpl <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;">/**</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"> * 认证类</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"> */</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>class</strong></span></strong><span style="color:#000000;"> UserDetailServiceImpl </span><strong><span style="color:#7f0055;"><strong>implements</strong></span></strong><span style="color:#000000;"> UserDetailsService { </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#646464;">@Override</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong><span style="color:#000000;"> UserDetails loadUserByUsername(String </span><span style="color:#6a3e3e;">username</span><span style="color:#000000;">) </span><strong><span style="color:#7f0055;"><strong>throws</strong></span></strong><span style="color:#000000;"> UsernameNotFoundException { </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f7f5f;">//构建角色集合</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">List<GrantedAuthority> </span><span style="color:#6a3e3e;">authorities</span><span style="color:#000000;">=</span><strong><u><span style="color:#7f0055;"><strong><u>new</u></strong></span></u></strong><u><span style="color:#000000;"><u> ArrayList()</u></span></u><span style="color:#000000;">;</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#6a3e3e;">authorities</span><span style="color:#000000;">.add(</span><strong><span style="color:#7f0055;"><strong>new</strong></span></strong><span style="color:#000000;"> SimpleGrantedAuthority(</span><span style="color:#2a00ff;">"ROLE_USER"</span><span style="color:#000000;">));</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>return</strong></span></strong> <strong><span style="color:#7f0055;"><strong>new</strong></span></strong><span style="color:#000000;"> User(</span><span style="color:#6a3e3e;">username</span><span style="color:#000000;">, </span><span style="color:#2a00ff;">""</span><span style="color:#000000;"> , </span><span style="color:#6a3e3e;">authorities</span><span style="color:#000000;">);</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p> </td> </tr> </tbody> </table> 这个类的主要作用是在登陆后得到用户名,可以根据用户名查询角色或执行一些逻辑。 ## **4.3获取登录名** ## 我们在处理后端逻辑需要获得登录名,那么如何获取单点登录的用户名呢? 其实和我们之前获得用户名的方式是完全相同的,我们下面来做个测试。 (1)web.xml 添加springmvc <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">servlet</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">servlet-name</span><span style="color:#008080;">></span><u><span style="color:#000000;"><u>springmvc</u></span></u><span style="color:#008080;"></</span><span style="color:#3f7f7f;">servlet-name</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">servlet-class</span><span style="color:#008080;">></span><span style="color:#000000;">org.springframework.web.servlet.DispatcherServlet</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">servlet-class</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#3f5fbf;"><!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载--></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span><span style="color:#000000;">contextConfigLocation</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-name</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span><span style="color:#000000;">classpath:springmvc.xml</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">param-value</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">init-param</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">servlet</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> </span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">servlet-mapping</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">servlet-name</span><span style="color:#008080;">></span><u><span style="color:#000000;"><u>springmvc</u></span></u><span style="color:#008080;"></</span><span style="color:#3f7f7f;">servlet-name</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span><span style="color:#000000;">*.do</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">url-pattern</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">servlet-mapping</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> (2)创建springmvc.xml <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">context:component-scan</span> <span style="color:#7f007f;">base-package</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"cn.itcast.demo"</em></span></em> <span style="color:#008080;">/></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">mvc:annotation-driven</span> <span style="color:#008080;">/></span></span></p> </td> </tr> </tbody> </table> (3)创建UserController <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#646464;">@RestController</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>class</strong></span></strong><span style="color:#000000;"> UserController { </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#646464;">@RequestMapping</span><span style="color:#000000;">(</span><span style="color:#2a00ff;">"/findLoginUser"</span><span style="color:#000000;">)</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>void</strong></span></strong><span style="color:#000000;"> findLoginUser(){ </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">String </span><span style="color:#6a3e3e;">name</span><span style="color:#000000;"> = SecurityContextHolder.</span><em><span style="color:#000000;"><em>getContext</em></span></em><span style="color:#000000;">().getAuthentication().getName();</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">System.</span><strong><em><span style="color:#0000c0;"><strong><em>out</em></strong></span></em></strong><span style="color:#000000;">.println(</span><span style="color:#6a3e3e;">name</span><span style="color:#000000;">);</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p> </td> </tr> </tbody> </table> 地址栏输入http://localhost:9003/findLoginUser.do 即可在控制台看到输出的登录名。 ## **4.4退出登录** ## 修改spring-security.xml <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"><beans:bean id=</span><em><span style="color:#7f7f7f;"><em>"requestSingleLogoutFilter"</em></span></em><span style="color:#7f7f7f;"> class=</span><em><span style="color:#7f7f7f;"><em>"org.springframework.security.web.authentication.logout.LogoutFilter"</em></span></em><span style="color:#7f7f7f;">> </span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#7f7f7f;"><beans:constructor-arg value=</span><em><span style="color:#7f7f7f;"><em>"http://localhost:9</em></span></em><em><span style="color:#7f7f7f;"><em>1</em></span></em><em><span style="color:#7f7f7f;"><em>00/cas/logout</em></span></em><em><span style="color:#ff0000;"><em>?service=http://localhost:9003/index2.html</em></span></em><em><span style="color:#7f7f7f;"><em>"</em></span></em><span style="color:#7f7f7f;">/> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"> <beans:constructor-arg> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"> <beans:bean class=</span><em><span style="color:#7f7f7f;"><em>"org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"</em></span></em><span style="color:#7f7f7f;">/> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"> </beans:constructor-arg> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"> <beans:property name=</span><em><span style="color:#7f7f7f;"><em>"filterProcessesUrl"</em></span></em><span style="color:#7f7f7f;"> value=</span><em><span style="color:#7f7f7f;"><em>"/logout/cas"</em></span></em><span style="color:#7f7f7f;">/> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"></beans:bean></span></span></p> </td> </tr> </tbody> </table> 在页面上添加链接 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">a</span> <span style="color:#7f007f;">href</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/logout/cas"</em></span></em><span style="color:#008080;">></span><span style="color:#000000;">退出登录</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">a</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> 创建index2.html,将index2.html设置为可匿名访问 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/index2.html"</em></span></em> <span style="color:#7f007f;">security</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"none"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> # **5.品优购用户中心** # ## **5.1需求分析** ## 用户中心实现单点登录。 ## **5.2代码实现** ## ### **5.2.1用户中心实现单点登录** ### 1. 将用户中心相关的页面(home-开头的)拷贝至 pinnyougou-user-web (2)pom.xml 引入springSecurity、cas客户端和springSecurity Cas整合包依赖(参照casclient\_demo3)。 (3)web.xml 添加spring-security过滤器(参照参照casclient\_demo3)设置首页为home-index.html <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">welcome-file-list</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">welcome-file</span><span style="color:#008080;">></span><span style="color:#000000;">home-index.html</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">welcome-file</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"></</span><span style="color:#3f7f7f;">welcome-file-list</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> (4)构建UserDetailsServiceImpl.java (参照casclient\_demo3) 1. 添加spring-security.xml(参照casclient\_demo3),并做以下修改 配置匿名访问资源 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#3f5fbf;"><!-- 匿名访问资源 --></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/css/**"</em></span></em> <span style="color:#7f007f;">security</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"none"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/js/**"</em></span></em> <span style="color:#7f007f;">security</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"none"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/image/**"</em></span></em> <span style="color:#7f007f;">security</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"none"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/plugins/**"</em></span></em> <span style="color:#7f007f;">security</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"none"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/register.html"</em></span></em> <span style="color:#7f007f;">security</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"none"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/user/add.do"</em></span></em> <span style="color:#7f007f;">security</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"none"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">http</span> <span style="color:#7f007f;">pattern</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/user/sendCode.do"</em></span></em> <span style="color:#7f007f;">security</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"none"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">http</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> 设置服务地址属性 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p style="margin-left:54pt;"><span style="color:#1e1c11;"><span style="color:#7f7f7f;"><</span><span style="color:#7f7f7f;">beans:bean</span><span style="color:#7f7f7f;"> id=</span><em><span style="color:#7f7f7f;"><em>"serviceProperties"</em></span></em><span style="color:#7f7f7f;"> class=</span><em><span style="color:#7f7f7f;"><em>"org.springframework.security.cas.ServiceProperties"</em></span></em><span style="color:#7f7f7f;">> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"> <beans:property name=</span><em><span style="color:#7f7f7f;"><em>"service"</em></span></em><span style="color:#7f7f7f;"> value=</span><em><span style="color:#7f7f7f;"><em>"http://localhost:</em></span></em><em><span style="color:#ff0000;"><em>910</em></span></em><em><span style="color:#ff0000;"><em>6</em></span></em><em><span style="color:#7f7f7f;"><em>/login/cas"</em></span></em><span style="color:#7f7f7f;">/></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"></</span><span style="color:#7f7f7f;">beans:bean</span><span style="color:#7f7f7f;">> </span></span></p> </td> </tr> </tbody> </table> 设置认证类 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"><beans:bean </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;">id=</span><em><span style="color:#7f7f7f;"><em>"userDetailsService"</em></span></em><span style="color:#7f7f7f;"> class=</span><em><span style="color:#7f7f7f;"><em>"</em></span></em><em><span style="color:#ff0000;"><em>com.pinyougou.user.service.UserDetailServiceImpl</em></span></em><em><span style="color:#7f7f7f;"><em>"</em></span></em><span style="color:#7f7f7f;">/> </span></span></p> </td> </tr> </tbody> </table> ### **5.2.2页面显示用户名** ### (1)pinyougou-user-web创建LoginController.java <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#646464;">@RestController</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#646464;">@RequestMapping</span><span style="color:#000000;">(</span><span style="color:#2a00ff;">"/login"</span><span style="color:#000000;">)</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <strong><span style="color:#7f0055;"><strong>class</strong></span></strong><span style="color:#000000;"> LoginController { </span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#646464;">@RequestMapping</span><span style="color:#000000;">(</span><span style="color:#2a00ff;">"/name"</span><span style="color:#000000;">)</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>public</strong></span></strong> <u><span style="color:#000000;"><u>Map</u></span></u><span style="color:#000000;"> showName(){ </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">String </span><span style="color:#6a3e3e;">name</span><span style="color:#000000;"> = SecurityContextHolder.</span><em><span style="color:#000000;"><em>getContext</em></span></em><span style="color:#000000;">().getAuthentication().getName();</span><span style="color:#3f7f5f;">//得到登陆人账号</span></span></p> <p><span style="color:#1e1c11;"><u><span style="color:#000000;"><u>Map</u></span></u> <span style="color:#6a3e3e;">map</span><span style="color:#000000;">=</span><strong><span style="color:#7f0055;"><strong>new</strong></span></strong><span style="color:#000000;"> HashMap<>();</span></span></p> <p><span style="color:#1e1c11;"><u><span style="color:#6a3e3e;"><u>map</u></span></u><u><span style="color:#000000;"><u>.put(</u></span></u><u><span style="color:#2a00ff;"><u>"loginName"</u></span></u><u><span style="color:#000000;"><u>, </u></span></u><u><span style="color:#6a3e3e;"><u>name</u></span></u><u><span style="color:#000000;"><u>)</u></span></u><span style="color:#000000;">;</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>return</strong></span></strong> <span style="color:#6a3e3e;">map</span><span style="color:#000000;">;</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p> </td> </tr> </tbody> </table> (2)创建loginService.js <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#3f7f5f;">//服务层</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">app.service(</span><span style="color:#2a00ff;">'loginService'</span><span style="color:#000000;">,</span><strong><span style="color:#7f0055;"><strong>function</strong></span></strong><span style="color:#000000;">($http){ </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#3f7f5f;">//读取列表数据绑定到表单中</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>this</strong></span></strong><span style="color:#000000;">.showName=</span><strong><span style="color:#7f0055;"><strong>function</strong></span></strong><span style="color:#000000;">(){ </span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>return</strong></span></strong><span style="color:#000000;"> $http.get(</span><span style="color:#2a00ff;">'../login/name.do'</span><span style="color:#000000;">);</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">});</span></span></p> </td> </tr> </tbody> </table> (3)创建indexController.js <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#3f7f5f;">//首页控制器</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">app.controller(</span><span style="color:#2a00ff;">'indexController'</span><span style="color:#000000;">,</span><strong><span style="color:#7f0055;"><strong>function</strong></span></strong><span style="color:#000000;">($scope,loginService){ </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">$scope.showName=</span><strong><span style="color:#7f0055;"><strong>function</strong></span></strong><span style="color:#000000;">(){ </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">loginService.showName().success(</span></span></p> <p><span style="color:#1e1c11;"><strong><span style="color:#7f0055;"><strong>function</strong></span></strong><span style="color:#000000;">(response){ </span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">$scope.loginName=response.loginName;</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span></span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">);</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">}</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#000000;">});</span></span></p> </td> </tr> </tbody> </table> (5)修改home-index.html 引入js <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">script</span> <span style="color:#7f007f;">type</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"text/javascript"</em></span></em> <span style="color:#7f007f;">src</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"plugins/angularjs/angular.min.js"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">script</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">script</span> <span style="color:#7f007f;">type</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"text/javascript"</em></span></em> <span style="color:#7f007f;">src</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"js/base.js"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">script</span><span style="color:#008080;">></span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">script</span> <span style="color:#7f007f;">type</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"text/javascript"</em></span></em> <span style="color:#7f007f;">src</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"js/service/loginService.js"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">script</span><span style="color:#008080;">></span></span></p> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">script</span> <span style="color:#7f007f;">type</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"text/javascript"</em></span></em> <span style="color:#7f007f;">src</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"js/controller/indexController.js"</em></span></em><span style="color:#008080;">></</span><span style="color:#3f7f7f;">script</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> 指令,调用方法查询登陆名 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">body</span> <u><span style="color:#7f007f;"><u>ng-app</u></span></u><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"pinyougou"</em></span></em> <u><span style="color:#7f007f;"><u>ng-controller</u></span></u><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"indexController"</em></span></em> <u><span style="color:#7f007f;"><u>ng-init</u></span></u><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"showName()"</em></span></em><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> 显示用户名 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">span</span> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"name"</em></span></em><span style="color:#008080;">></span><span style="color:#000000;">{ {loginName}}</span><span style="color:#008080;"></</span><span style="color:#3f7f7f;">span</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> ### **5.2.3退出登录** ### 设置退出登录后的跳转地址 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"><</span><span style="color:#7f7f7f;">beans:bean</span><span style="color:#7f7f7f;"> id=</span><em><span style="color:#7f7f7f;"><em>"requestSingleLogoutFilter"</em></span></em><span style="color:#7f7f7f;"> class=</span><em><span style="color:#7f7f7f;"><em>"org.springframework.security.web.authentication.logout.LogoutFilter"</em></span></em><span style="color:#7f7f7f;">> </span></span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"> <beans:constructor-arg value=</span><em><span style="color:#7f7f7f;"><em>"http://localhost:9100/cas/logout</em></span></em><em><span style="color:#ff0000;"><em>?service=http://</em></span></em><em><span style="color:#ff0000;"><em>localhost:9103</em></span></em><em><span style="color:#7f7f7f;"><em>"</em></span></em><span style="color:#7f7f7f;">/> </span></span></p> <p><span style="color:#1e1c11;"> <span style="color:#7f7f7f;">........</span> </span></p> <p><span style="color:#1e1c11;"><span style="color:#7f7f7f;"></</span><span style="color:#7f7f7f;">beans:bean</span><span style="color:#7f7f7f;">> </span></span></p> </td> </tr> </tbody> </table> 退出登录后,跳转到网站首页 <table style="width:426.1pt;"> <tbody> <tr> <td style="vertical-align:top;width:426.1pt;"> <p><span style="color:#1e1c11;"><span style="color:#008080;"><</span><span style="color:#3f7f7f;">span</span> <span style="color:#7f007f;">class</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"safe"</em></span></em><span style="color:#008080;">></span> <span style="color:#008080;"><</span><span style="color:#3f7f7f;">a</span> <span style="color:#7f007f;">href</span><span style="color:#000000;">=</span><em><span style="color:#2a00ff;"><em>"/logout/cas"</em></span></em><span style="color:#008080;">></span><span style="color:#000000;">退出登录</span> <span style="color:#008080;"></</span><span style="color:#3f7f7f;">a</span><span style="color:#008080;">></</span><span style="color:#3f7f7f;">span</span><span style="color:#008080;">></span></span></p> </td> </tr> </tbody> </table> ## **附录A. Spring Security 内置过滤器表** ## <table style="margin-left:4.65pt;width:433.5pt;"> <tbody> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><strong><span style="color:#1e1c11;"><strong>别名</strong></span></strong></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><strong><span style="color:#1e1c11;"><strong>Filter 类</strong></span></strong></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">CHANNEL_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">ChannelProcessingFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">SECURITY_CONTEXT_FILTER</span></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">SecurityContextPersistenceFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">CONCURRENT_SESSION_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">ConcurrentSessionFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">LOGOUT_FILTER</span></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">LogoutFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">X509_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">X509AuthenticationFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">PRE_AUTH_FILTER</span></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">AstractPreAuthenticatedProcessingFilter 的子类</span></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">CAS_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">CasAuthenticationFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">FORM_LOGIN_FILTER</span></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">UsernamePasswordAuthenticationFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">BASIC_AUTH_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">BasicAuthenticationFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">SERVLET_API_SUPPORT_FILTER</span></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">SecurityContextHolderAwareRequestFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">JAAS_API_SUPPORT_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">JaasApiIntegrationFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">REMEMBER_ME_FILTER</span></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">RememberMeAuthenticationFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">ANONYMOUS_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">AnonymousAuthenticationFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">SESSION_MANAGEMENT_FILTER</span></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">SessionManagementFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">EXCEPTION_TRANSLATION_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">ExceptionTranslationFilter</span></span></p> </td> </tr> <tr> <td style="background-color:#ffffff;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">FILTER_SECURITY_INTERCEPTOR</span></span></p> </td> <td style="background-color:#ffffff;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">FilterSecurityInterceptor</span></span></p> </td> </tr> <tr> <td style="background-color:#f8f8f8;width:182.15pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">SWITCH_USER_FILTER</span></span></p> </td> <td style="background-color:#f8f8f8;width:251.35pt;"> <p><span style="color:#1e1c11;"><span style="color:#1e1c11;">SwitchUserFilter</span></span></p> </td> </tr> </tbody> </table> [spring]: http://lib.csdn.net/base/javaee [http_localhost_9100_cas_logout]: http://localhost:9000/cas/logout
相关 单点登录 Shiro 1.2 开始提供了 Jasig CAS 单点登录的支持,单点登录主要用于多系统集成,即在多个 系统中,用户只需要到一个中央服务器登录一次即可访问 比眉伴天荒/ 2023年07月08日 14:25/ 0 赞/ 9 阅读
相关 单点登录 官网 https://apereo.github.io/cas/4.2.x/planning/Architecture.html demo http://www.i 我不是女神ヾ/ 2022年11月03日 10:45/ 0 赞/ 220 阅读
相关 单点登录 业务需要,正在研究. 基本有以下三种方案 1.集中管理Session. 实现很简单,但缺点是 每次页面刷新都需要访问一次或两次 Session管理服务器, 水深无声/ 2022年09月19日 10:55/ 0 赞/ 226 阅读
相关 单点登录 1:什么事单点登录: SSO英文全称Single Sign On,单点登录。SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。它包括可以将这次主 男娘i/ 2022年07月13日 14:29/ 0 赞/ 216 阅读
相关 单点登录 一、单系统登录机制 1、http无状态协议 web应用采用browser/server架构,http作为通信协议。http是无状态协议,浏览器的每一次请求,服务 港控/mmm°/ 2022年06月11日 00:55/ 0 赞/ 230 阅读
相关 单点登录 项目分为三个模块 server client2 client1 server提供的接口 /user /tol 深藏阁楼爱情的钟/ 2022年05月24日 04:26/ 0 赞/ 263 阅读
相关 单点登录 1.3 CAS服务端部署 Cas服务端其实就是一个war包。 在资源\\cas\\source\\cas-server-4.0.0-release\\cas-serve 梦里梦外;/ 2022年05月16日 08:04/ 0 赞/ 285 阅读
相关 单点登录 初识单点登录 最初接触到单点登录要追溯到3年多以前了,那时候看到的只是passport,当时要做全国所有社区的登录,然后就照着内部文档写了代码,然后就接入了(这里要提一句 谁借莪1个温暖的怀抱¢/ 2022年05月13日 14:04/ 0 赞/ 265 阅读
相关 单点登录 以Cookie作为凭证媒介 最简单的单点登录实现方式,是使用cookie作为媒介,存放用户凭证。 用户登录父应用之后,应用返回一个加密的cookie,当用户访问子应用 £神魔★判官ぃ/ 2022年04月23日 13:18/ 0 赞/ 299 阅读
相关 单点登录 什么是单点登录? 单点登录(Single Sign On)说的简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其它系统中登录。也就是用户的一次登录能得到其它所有 布满荆棘的人生/ 2021年11月11日 04:10/ 0 赞/ 441 阅读
还没有评论,来说两句吧...