<commit_before>---
layout: post
title: Spring security
---

To explain the steps and configurations for Spring to CAS integration.

### Spring security filter configurations

Add a custom filter in the security.xm

	        <custom-filter after="ANONYMOUS_FILTER" ref="cas_anonymousLoginFilterChain"/>
			<custom-filter position="CAS_FILTER" ref="casFilter" />
			

		     <beans:bean id="cas_anonymousLoginFilter"
		                 class="com.mozanta.portal.filter.CasAnonymousLoginFilter">
		         <beans:property name="authenticationProvider" ref="casAuthenticationProvider"/>
		         <beans:property name="ticketRetriever" ref="serviceTicketRetriever"/>
		         <beans:property name="user" value="${portal.anonymous.username}"/>
		     </beans:bean>

		     <beans:bean id="serviceTicketRetriever"
		                 class="com.mozanta.portal.authentication.ServiceTicketRetriever">
		         <beans:property name="url" value="${cas.server.url}/v1/tickets"/>
		         <beans:property name="serviceProperties" ref="serviceProperties"/>
		         <beans:property name="userDetailsService" ref="portalUserDetailsService"/>
		     </beans:bean>

		    <beans:bean id="casFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
		         <beans:property name="authenticationManager" ref="authenticationManager"/>
		         <beans:property name="authenticationSuccessHandler" ref="customAuthenticationSuccessHandler"/>
		         <beans:property name="proxyGrantingTicketStorage" ref="pgtStorage" />
		         <beans:property name="proxyReceptorUrl" value="/j_spring_cas_security_proxyreceptor" />
		     </beans:bean>

		     <beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
		         <beans:property name="authenticateAllArtifacts" value="true" />
		         <beans:property name="service" value="${cas.service.url}/j_spring_security_check"/>
		         <beans:property name="sendRenew" value="false"/>
		     </beans:bean>

		     <!-- CAS LOGOUT -->
		     <!-- This filter handles a Single Logout Request from the CAS Server -->
		     <beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>

		     <!-- This filter redirects to the CAS Server to signal Single Logout should be performed -->
		     <beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
		         <beans:constructor-arg name="logoutSuccessHandler" ref="customSpecialPageHandler" />
		         <beans:constructor-arg>
		             <beans:array>
		                 <beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
		                 <beans:bean class="com.mozanta.portal.authentication.logout.PortalCasLogoutHandler">
		                     <beans:property name="casRestUri" value="${cas.server.url}/v1/tickets" />
		                 </beans:bean>
		             </beans:array>
		         </beans:constructor-arg>
		     </beans:bean>

### Spring security configurations

	<authentication-manager alias="authenticationManager">
	        <authentication-provider ref="casAuthenticationProvider"/>
	    </authentication-manager>

	    <beans:bean id="casAuthenticationProvider" class="com.innovent.store.authentication.PortalCasAuthenticationProvider">
	        <beans:property name="authenticationUserDetailsService">
	            <beans:bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
	                <beans:constructor-arg ref="userDetailsService" />
	            </beans:bean>
	        </beans:property>
	        <beans:property name="serviceProperties" ref="serviceProperties" />
	        <beans:property name="ticketValidator">
	            <beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
	                <beans:constructor-arg value="${cas.server.url}" />
	                <beans:property name="proxyCallbackUrl" value="${cas.service.url}/j_spring_cas_security_proxyreceptor"/>
	                <beans:property name="proxyGrantingTicketStorage" ref="pgtStorage" />
	            </beans:bean>
	        </beans:property>
	        <beans:property name="key" value="system_cas"/>
	        <beans:property name="ticketRetriever" ref="serviceTicketRetriever"/>
	        <beans:property name="passwordEncoder" ref="passwordEncoder" />
	        <beans:property name="authenticationService" ref="authenticationService" />
	    </beans:bean>

	    <beans:bean id="pgtStorage" class="org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl" />
	
### Portal CAS Authentication Provider

public class MosaicCasAuthenticationProvider extends PortalAuthenticationProvider {
    private MosaicGrantedAuthoritiesProvider grantedAuthoritiesProvider;

    @Override
    public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
        final CasAuthenticationToken auth = (CasAuthenticationToken) super.authenticate(authentication);
        final Set<GrantedAuthority> authorities = grantedAuthoritiesProvider.provide((Principal) auth.getPrincipal());
        return new CasAuthenticationToken(this.getKey(), auth.getUserDetails(), authentication.getCredentials(),
                authorities, auth.getUserDetails(), auth.getAssertion());
    }

    public void setGrantedAuthoritiesProvider(MosaicGrantedAuthoritiesProvider grantedAuthoritiesProvider) {
        this.grantedAuthoritiesProvider = grantedAuthoritiesProvider;
    }
}

### Service Ticket Retriever


	import java.io.IOException;<commit_msg>updated cas integration with Authentication provider<commit_after>---
layout: post
title: Spring security
---

To explain the steps and configurations for Spring to CAS integration.

### Spring security filter configurations

Add a custom filter in the security.xm

	        <custom-filter after="ANONYMOUS_FILTER" ref="cas_anonymousLoginFilterChain"/>
			<custom-filter position="CAS_FILTER" ref="casFilter" />
			

		     <beans:bean id="cas_anonymousLoginFilter"
		                 class="com.mozanta.portal.filter.CasAnonymousLoginFilter">
		         <beans:property name="authenticationProvider" ref="casAuthenticationProvider"/>
		         <beans:property name="ticketRetriever" ref="serviceTicketRetriever"/>
		         <beans:property name="user" value="${portal.anonymous.username}"/>
		     </beans:bean>

		     <beans:bean id="serviceTicketRetriever"
		                 class="com.mozanta.portal.authentication.ServiceTicketRetriever">
		         <beans:property name="url" value="${cas.server.url}/v1/tickets"/>
		         <beans:property name="serviceProperties" ref="serviceProperties"/>
		         <beans:property name="userDetailsService" ref="portalUserDetailsService"/>
		     </beans:bean>

		    <beans:bean id="casFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
		         <beans:property name="authenticationManager" ref="authenticationManager"/>
		         <beans:property name="authenticationSuccessHandler" ref="customAuthenticationSuccessHandler"/>
		         <beans:property name="proxyGrantingTicketStorage" ref="pgtStorage" />
		         <beans:property name="proxyReceptorUrl" value="/j_spring_cas_security_proxyreceptor" />
		     </beans:bean>

		     <beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
		         <beans:property name="authenticateAllArtifacts" value="true" />
		         <beans:property name="service" value="${cas.service.url}/j_spring_security_check"/>
		         <beans:property name="sendRenew" value="false"/>
		     </beans:bean>

		     <!-- CAS LOGOUT -->
		     <!-- This filter handles a Single Logout Request from the CAS Server -->
		     <beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>

		     <!-- This filter redirects to the CAS Server to signal Single Logout should be performed -->
		     <beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
		         <beans:constructor-arg name="logoutSuccessHandler" ref="customSpecialPageHandler" />
		         <beans:constructor-arg>
		             <beans:array>
		                 <beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
		                 <beans:bean class="com.mozanta.portal.authentication.logout.PortalCasLogoutHandler">
		                     <beans:property name="casRestUri" value="${cas.server.url}/v1/tickets" />
		                 </beans:bean>
		             </beans:array>
		         </beans:constructor-arg>
		     </beans:bean>

### Spring security configurations

	<authentication-manager alias="authenticationManager">
	        <authentication-provider ref="casAuthenticationProvider"/>
	    </authentication-manager>

	    <beans:bean id="casAuthenticationProvider" class="com.innovent.store.authentication.PortalCasAuthenticationProvider">
	        <beans:property name="authenticationUserDetailsService">
	            <beans:bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
	                <beans:constructor-arg ref="userDetailsService" />
	            </beans:bean>
	        </beans:property>
	        <beans:property name="serviceProperties" ref="serviceProperties" />
	        <beans:property name="ticketValidator">
	            <beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
	                <beans:constructor-arg value="${cas.server.url}" />
	                <beans:property name="proxyCallbackUrl" value="${cas.service.url}/j_spring_cas_security_proxyreceptor"/>
	                <beans:property name="proxyGrantingTicketStorage" ref="pgtStorage" />
	            </beans:bean>
	        </beans:property>
	        <beans:property name="key" value="system_cas"/>
	        <beans:property name="ticketRetriever" ref="serviceTicketRetriever"/>
	        <beans:property name="passwordEncoder" ref="passwordEncoder" />
	        <beans:property name="authenticationService" ref="authenticationService" />
	    </beans:bean>

	    <beans:bean id="pgtStorage" class="org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl" />
	
### Portal CAS Authentication Provider

    public class MosaicCasAuthenticationProvider extends PortalAuthenticationProvider {
    private MosaicGrantedAuthoritiesProvider grantedAuthoritiesProvider;

    @Override
    public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
        final CasAuthenticationToken auth = (CasAuthenticationToken) super.authenticate(authentication);
        final Set<GrantedAuthority> authorities = grantedAuthoritiesProvider.provide((Principal) auth.getPrincipal());
        return new CasAuthenticationToken(this.getKey(), auth.getUserDetails(), authentication.getCredentials(),
                authorities, auth.getUserDetails(), auth.getAssertion());
    }

    public void setGrantedAuthoritiesProvider(MosaicGrantedAuthoritiesProvider grantedAuthoritiesProvider) {
        this.grantedAuthoritiesProvider = grantedAuthoritiesProvider;
    }
}

### Service Ticket Retriever


	import java.io.IOException;
{'fraction_non_alphanumeric': 0.11412419397579719, 'fraction_numerical': 0.0007066513558872891, 'mean_word_length': 3.7096505823627286, 'pattern_counts': {'":': 0, '<': 127, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4814212', 'n_tokens_mistral': 3086, 'n_tokens_neox': 2773, 'n_words': 482}