SEC-2194: Add Java Config samples
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.security.samples.cas.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.jasig.cas.client.util.CommonUtils;
|
||||
import org.springframework.security.cas.authentication.CasAuthenticationToken;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* {@link ProxyTicketSampleServlet} demonstrates how to obtain a proxy ticket
|
||||
* and then use it to make a remote call. To learn how proxy tickets work, see
|
||||
* the <a href="https://wiki.jasig.org/display/CAS/Proxy+CAS+Walkthrough">Proxy
|
||||
* CAS Walkthrough</a>
|
||||
* </p>
|
||||
*
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public final class ProxyTicketSampleServlet extends HttpServlet {
|
||||
/**
|
||||
* This is the URL that will be called and authenticate a proxy ticket.
|
||||
*/
|
||||
private String targetUrl;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
// NOTE: The CasAuthenticationToken can also be obtained using SecurityContextHolder.getContext().getAuthentication()
|
||||
final CasAuthenticationToken token = (CasAuthenticationToken) request.getUserPrincipal();
|
||||
// proxyTicket could be reused to make calls to to the CAS service even if the target url differs
|
||||
final String proxyTicket = token.getAssertion().getPrincipal().getProxyTicketFor(targetUrl);
|
||||
|
||||
// Make a remote call to ourself. This is a bit silly, but it works well to demonstrate how to use proxy tickets.
|
||||
final String serviceUrl = targetUrl+"?ticket="+URLEncoder.encode(proxyTicket, "UTF-8");
|
||||
String proxyResponse = CommonUtils.getResponseFromServer(serviceUrl, "UTF-8");
|
||||
|
||||
// modify the response and write it out to inform the user that it was obtained using a proxy ticket.
|
||||
proxyResponse = proxyResponse.replaceFirst("Secure Page", "Secure Page using a Proxy Ticket");
|
||||
proxyResponse = proxyResponse.replaceFirst("<p>",
|
||||
"<p>This page is rendered by "+getClass().getSimpleName()+" by making a remote call to the Secure Page using a proxy ticket ("+proxyTicket+") and inserts this message. ");
|
||||
final PrintWriter writer = response.getWriter();
|
||||
writer.write(proxyResponse);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the target URL. It allows for the host to change based upon
|
||||
* the "cas.service.host" system property. If the property is not set, the
|
||||
* default is "localhost:8443".
|
||||
*/
|
||||
@Override
|
||||
public void init() throws ServletException {
|
||||
super.init();
|
||||
String casServiceHost = System.getProperty("cas.service.host", "localhost:8443");
|
||||
targetUrl = "https://"+casServiceHost+"/cas-sample/secure/";
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -7720161771819727775L;
|
||||
}
|
||||
8
samples/cas/sample-xml/src/main/webapp/403.jsp
Normal file
8
samples/cas/sample-xml/src/main/webapp/403.jsp
Normal file
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>403 - Access Denied</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>403 - Access Denied</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,118 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
|
||||
xmlns="http://www.springframework.org/schema/security"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<http entry-point-ref="casEntryPoint" use-expressions="true">
|
||||
<intercept-url pattern="/" access="permitAll"/>
|
||||
<intercept-url pattern="/index.jsp" access="permitAll"/>
|
||||
<intercept-url pattern="/cas-logout.jsp" access="permitAll"/>
|
||||
<intercept-url pattern="/casfailed.jsp" access="permitAll"/>
|
||||
|
||||
<intercept-url pattern="/secure/extreme/**"
|
||||
access="hasRole('ROLE_SUPERVISOR')" />
|
||||
<intercept-url pattern="/secure/**" access="hasRole('ROLE_USER')" />
|
||||
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
|
||||
<custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER"/>
|
||||
<custom-filter ref="singleLogoutFilter" before="CAS_FILTER"/>
|
||||
<custom-filter ref="casFilter" position="CAS_FILTER" />
|
||||
<logout logout-success-url="/cas-logout.jsp"/>
|
||||
</http>
|
||||
|
||||
<authentication-manager alias="authManager">
|
||||
<authentication-provider ref="casAuthProvider" />
|
||||
</authentication-manager>
|
||||
|
||||
<user-service id="userService">
|
||||
<user name="rod" password="rod" authorities="ROLE_SUPERVISOR,ROLE_USER" />
|
||||
<user name="dianne" password="dianne" authorities="ROLE_USER" />
|
||||
<user name="scott" password="scott" authorities="ROLE_USER" />
|
||||
</user-service>
|
||||
|
||||
<!-- This filter handles a Single Logout Request from the CAS Server -->
|
||||
<b:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>
|
||||
<!-- This filter redirects to the CAS Server to signal Single Logout should be performed -->
|
||||
<b:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter"
|
||||
p:filterProcessesUrl="/j_spring_cas_security_logout">
|
||||
<b:constructor-arg value="https://${cas.server.host}/cas/logout"/>
|
||||
<b:constructor-arg>
|
||||
<b:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>
|
||||
</b:constructor-arg>
|
||||
</b:bean>
|
||||
|
||||
<b:bean id="serviceProperties"
|
||||
class="org.springframework.security.cas.ServiceProperties"
|
||||
p:service="https://${cas.service.host}/cas-sample/j_spring_cas_security_check"
|
||||
p:authenticateAllArtifacts="true"/>
|
||||
<b:bean id="casEntryPoint"
|
||||
class="org.springframework.security.cas.web.CasAuthenticationEntryPoint"
|
||||
p:serviceProperties-ref="serviceProperties" p:loginUrl="https://${cas.server.host}/cas/login" />
|
||||
<b:bean id="casFilter"
|
||||
class="org.springframework.security.cas.web.CasAuthenticationFilter"
|
||||
p:authenticationManager-ref="authManager"
|
||||
p:serviceProperties-ref="serviceProperties"
|
||||
p:proxyGrantingTicketStorage-ref="pgtStorage"
|
||||
p:proxyReceptorUrl="/j_spring_cas_security_proxyreceptor">
|
||||
<b:property name="authenticationDetailsSource">
|
||||
<b:bean class="org.springframework.security.cas.web.authentication.ServiceAuthenticationDetailsSource"/>
|
||||
</b:property>
|
||||
<b:property name="authenticationFailureHandler">
|
||||
<b:bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"
|
||||
p:defaultFailureUrl="/casfailed.jsp"/>
|
||||
</b:property>
|
||||
</b:bean>
|
||||
<!--
|
||||
NOTE: In a real application you should not use an in memory implementation. You will also want
|
||||
to ensure to clean up expired tickets by calling ProxyGrantingTicketStorage.cleanup()
|
||||
-->
|
||||
<b:bean id="pgtStorage" class="org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl"/>
|
||||
<b:bean id="casAuthProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider"
|
||||
p:serviceProperties-ref="serviceProperties"
|
||||
p:key="casAuthProviderKey">
|
||||
<b:property name="authenticationUserDetailsService">
|
||||
<b:bean
|
||||
class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
|
||||
<b:constructor-arg ref="userService" />
|
||||
</b:bean>
|
||||
</b:property>
|
||||
<b:property name="ticketValidator">
|
||||
<b:bean
|
||||
class="org.jasig.cas.client.validation.Cas20ProxyTicketValidator"
|
||||
p:acceptAnyProxy="true"
|
||||
p:proxyCallbackUrl="https://${cas.service.host}/cas-sample/j_spring_cas_security_proxyreceptor"
|
||||
p:proxyGrantingTicketStorage-ref="pgtStorage">
|
||||
<b:constructor-arg value="https://${cas.server.host}/cas" />
|
||||
</b:bean>
|
||||
</b:property>
|
||||
<b:property name="statelessTicketCache">
|
||||
<b:bean class="org.springframework.security.cas.authentication.EhCacheBasedTicketCache">
|
||||
<b:property name="cache">
|
||||
<b:bean class="net.sf.ehcache.Cache"
|
||||
init-method="initialise"
|
||||
destroy-method="dispose">
|
||||
<b:constructor-arg value="casTickets"/>
|
||||
<b:constructor-arg value="50"/>
|
||||
<b:constructor-arg value="true"/>
|
||||
<b:constructor-arg value="false"/>
|
||||
<b:constructor-arg value="3600"/>
|
||||
<b:constructor-arg value="900"/>
|
||||
</b:bean>
|
||||
</b:property>
|
||||
</b:bean>
|
||||
</b:property>
|
||||
</b:bean>
|
||||
|
||||
<!-- Configuration for the environment can be overriden by system properties -->
|
||||
<context:property-placeholder system-properties-mode="OVERRIDE" properties-ref="environment"/>
|
||||
<util:properties id="environment">
|
||||
<b:prop key="cas.service.host">localhost:8443</b:prop>
|
||||
<b:prop key="cas.server.host">localhost:9443</b:prop>
|
||||
</util:properties>
|
||||
</b:beans>
|
||||
85
samples/cas/sample-xml/src/main/webapp/WEB-INF/web.xml
Normal file
85
samples/cas/sample-xml/src/main/webapp/WEB-INF/web.xml
Normal file
@@ -0,0 +1,85 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
- Tutorial web application
|
||||
-
|
||||
-->
|
||||
|
||||
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
|
||||
<display-name>Spring Security CAS Demo Application</display-name>
|
||||
|
||||
<!--
|
||||
- Location of the XML file that defines the root application context
|
||||
- Applied by ContextLoaderListener.
|
||||
-->
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>
|
||||
/WEB-INF/applicationContext-security.xml
|
||||
</param-value>
|
||||
</context-param>
|
||||
|
||||
<context-param>
|
||||
<param-name>webAppRootKey</param-name>
|
||||
<param-value>cas.root</param-value>
|
||||
</context-param>
|
||||
|
||||
<!--
|
||||
Include the character encoding Filter as per JASIG recommenation when doing Single Sign Out
|
||||
https://wiki.jasig.org/display/CASC/Configuring+Single+Sign+Out
|
||||
-->
|
||||
<filter>
|
||||
<filter-name>characterEncodingFilter</filter-name>
|
||||
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
|
||||
<init-param>
|
||||
<param-name>encoding</param-name>
|
||||
<param-value>UTF-8</param-value>
|
||||
</init-param>
|
||||
</filter>
|
||||
<filter>
|
||||
<filter-name>springSecurityFilterChain</filter-name>
|
||||
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
|
||||
</filter>
|
||||
|
||||
<filter-mapping>
|
||||
<filter-name>characterEncodingFilter</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
<filter-mapping>
|
||||
<filter-name>springSecurityFilterChain</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<!--
|
||||
Included to support Single Logout. Note that the SingleSignOutFilter is included in the
|
||||
springSecurityFilterChain. However, it could also be placed as the first filter-mapping
|
||||
in the web.xml
|
||||
-->
|
||||
<listener>
|
||||
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!--
|
||||
- Loads the root application context of this web app at startup.
|
||||
- The application context is then available via
|
||||
- WebApplicationContextUtils.getWebApplicationContext(servletContext).
|
||||
-->
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>ptSampleServlet</servlet-name>
|
||||
<servlet-class>org.springframework.security.samples.cas.web.ProxyTicketSampleServlet</servlet-class>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>ptSampleServlet</servlet-name>
|
||||
<url-pattern>/secure/ptSample</url-pattern>
|
||||
</servlet-mapping>
|
||||
<error-page>
|
||||
<error-code>403</error-code>
|
||||
<location>/403.jsp</location>
|
||||
</error-page>
|
||||
</web-app>
|
||||
15
samples/cas/sample-xml/src/main/webapp/cas-logout.jsp
Normal file
15
samples/cas/sample-xml/src/main/webapp/cas-logout.jsp
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Single-sign out?</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2>Do you want to log out of CAS?</h2>
|
||||
|
||||
<p>You have logged out of this application, but may still have an active single-sign on session with CAS.</p>
|
||||
|
||||
<p><a href="j_spring_cas_security_logout">Logout of CAS</a></p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
26
samples/cas/sample-xml/src/main/webapp/casfailed.jsp
Normal file
26
samples/cas/sample-xml/src/main/webapp/casfailed.jsp
Normal file
@@ -0,0 +1,26 @@
|
||||
<%@ page import="org.springframework.security.core.AuthenticationException" %>
|
||||
<%@ page import="org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter" %>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Login to CAS failed!</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2>Login to CAS failed!</h2>
|
||||
|
||||
<font color="red">
|
||||
Your CAS credentials were rejected.<br/><br/>
|
||||
Reason:
|
||||
<%
|
||||
Exception error = ((AuthenticationException) session.getAttribute(AbstractAuthenticationProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY));
|
||||
if(error != null) {
|
||||
%>
|
||||
<%= error.getMessage() %>
|
||||
<%
|
||||
}
|
||||
%>
|
||||
</font>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
12
samples/cas/sample-xml/src/main/webapp/index.jsp
Normal file
12
samples/cas/sample-xml/src/main/webapp/index.jsp
Normal file
@@ -0,0 +1,12 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>Home Page</h1>
|
||||
<p>Anyone can view this page.</p>
|
||||
|
||||
<p>Your principal object is....: <%= request.getUserPrincipal() %></p>
|
||||
|
||||
<p><a href="secure/index.jsp">Secure page</a></p>
|
||||
<p><a href="secure/ptSample">Proxy Ticket Sample page</a></p>
|
||||
<p><a href="secure/extreme/index.jsp">Extremely secure page</a></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>VERY Secure Page</h1>
|
||||
This is a protected page. You can only see me if you are a supervisor.
|
||||
|
||||
<p><a href="../../">Home</a>
|
||||
<p><a href="../../secure/index.jsp">Secure page</a></p>
|
||||
<p><a href="../../secure/ptSample">Proxy Ticket Sample page</a></p>
|
||||
<p><a href="../../j_spring_security_logout">Logout</a>
|
||||
</body>
|
||||
</html>
|
||||
15
samples/cas/sample-xml/src/main/webapp/secure/index.jsp
Normal file
15
samples/cas/sample-xml/src/main/webapp/secure/index.jsp
Normal file
@@ -0,0 +1,15 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>Secure Page</h1>
|
||||
<p>This is a protected page. You can get to me if you've been remembered,
|
||||
or if you've authenticated this session.</p>
|
||||
|
||||
<%if (request.isUserInRole("ROLE_SUPERVISOR")) { %>
|
||||
<p>You are a supervisor! You can therefore see the <a href="extreme/index.jsp">extremely secure page</a>.</p>
|
||||
<% } %>
|
||||
|
||||
<p><a href="../">Home</a>
|
||||
<p><a href="ptSample">Proxy Ticket Sample page</a></p>
|
||||
<p><a href="../j_spring_security_logout">Logout</a>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user