简体   繁体   English

无法使CAS Single Sign Out与Spring Security一起使用

[英]Can't make CAS Single Sign Out work with Spring Security

I'm not finding any actual guides as to implement the Single Sign Out CAS feature on my apps. 我没有找到有关在我的应用程序中实现Single Sign Out CAS功能的实际指南。 I've tried a number of answers here on SO, but none worked(like this and this ). 我在SO上尝试了许多答案,但是没有一个有效(像thisthis )。 Also, there's no examples to be found of the Spring Security+CAS using the Java configuration, so i'm also a bit lost on that. 另外,没有找到使用Java配置的Spring Security + CAS的示例,因此我对此也有所遗漏。 I cannot even figure out if this is the actual URL that i should be using, as the documentation tells me to use "/j_spring_security_logout", and that's just redirecting me to a blank index page, as my index page is working if i access it normally(albeit the console shows all the correct requests, like the JS and CSS). 我什至无法确定这是否是我应该使用的实际URL,因为文档告诉我使用“ / j_spring_security_logout”,这只是将我重定向到空白索引页面,因为如果我访问它,索引页面可以正常工作通常(尽管控制台会显示所有正确的请求,例如JS和CSS)。 Would really appreciate some guidance, as there's NO documentation that i could find that are using the Java annotation. 真的希望您能提供一些指导,因为我找不到使用Java注释的文档。 Thanks in advance! 提前致谢!

My WebSecurityConfig: 我的WebSecurityConfig:

@Configuration

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private static String CAS_URL = "https://localhost:8443/cas";
    private static String APP_URL = "https://localhost:8443/i9t-YM";

    @Bean
    public ServiceProperties serviceProperties() {
        ServiceProperties serviceProperties = new ServiceProperties();
        serviceProperties.setService(APP_URL+"/j_spring_cas_security_check");
        serviceProperties.setSendRenew(false);
        return serviceProperties;
    }

    @Bean
    public CasAuthenticationProvider casAuthenticationProvider() {
        CasAuthenticationProvider casAuthenticationProvider = new CasAuthenticationProvider();
        casAuthenticationProvider.setAuthenticationUserDetailsService(authenticationUserDetailsService());
        casAuthenticationProvider.setServiceProperties(serviceProperties());
        casAuthenticationProvider.setTicketValidator(cas20ServiceTicketValidator());
        casAuthenticationProvider.setKey("an_id_for_this_auth_provider_only");
        return casAuthenticationProvider;
    }

    @Bean
    public AuthenticationUserDetailsService authenticationUserDetailsService() {
        return new TestCasAuthenticationUserDetailsService();
    }

    @Bean
    public Cas20ServiceTicketValidator cas20ServiceTicketValidator() {
        return new Cas20ServiceTicketValidator(CAS_URL);
    }

    @Bean
    public CasAuthenticationFilter casAuthenticationFilter() throws Exception {
        CasAuthenticationFilter casAuthenticationFilter = new CasAuthenticationFilter();
        casAuthenticationFilter.setAuthenticationManager(authenticationManager());
        return casAuthenticationFilter;
    }

    @Bean
    public CasAuthenticationEntryPoint casAuthenticationEntryPoint() {
        CasAuthenticationEntryPoint casAuthenticationEntryPoint = new CasAuthenticationEntryPoint();
        casAuthenticationEntryPoint.setLoginUrl(CAS_URL+"/login");
        casAuthenticationEntryPoint.setServiceProperties(serviceProperties());
        return casAuthenticationEntryPoint;
    }

    @Bean
    public SingleSignOutFilter SingleSignOutFilter(){
        return new SingleSignOutFilter();
    }

    @Bean
    public LogoutFilter requestLogoutFilter(){
        SecurityContextLogoutHandler handler = new SecurityContextLogoutHandler();
        handler.setClearAuthentication(true);
        handler.setInvalidateHttpSession(true);
        LogoutFilter logoutFilter = new LogoutFilter(APP_URL, handler);
        return logoutFilter;
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(casAuthenticationProvider());
        auth.inMemoryAuthentication().withUser("joe").password("joe").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilter(casAuthenticationFilter());
        http.exceptionHandling().authenticationEntryPoint(casAuthenticationEntryPoint());
        http.addFilterBefore(requestLogoutFilter(), LogoutFilter.class);
        http.addFilterBefore(SingleSignOutFilter(), CasAuthenticationFilter.class);
        http.httpBasic().and().authorizeRequests().antMatchers("/index.html", "/home.html", "/login.html", "/")
                .permitAll().anyRequest().authenticated()
        .and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
                .csrf().csrfTokenRepository(csrfTokenRepository())
                ;
        http.logout()
        .deleteCookies("remove").invalidateHttpSession(true).logoutUrl("cas/logout")
        .logoutSuccessUrl("/");
        //http.exceptionHandling().accessDeniedPage("/403.html");
    }

    private CsrfTokenRepository csrfTokenRepository() {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
    }

}

The SSOut Filter on my Web.xml, dunno exactly why i added it: Web.xml上的SSOut筛选器,不知道我为什么添加它:

<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-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
  <listener-class>
    org.jasig.cas.client.session.SingleSignOutHttpSessionListener
  </listener-class>
</listener>

This is my configuration for single sign out over spring security with cas integration: 这是我通过cas集成进行Spring Security单次注销的配置:

<bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter" />

<bean id="requestSingleLogoutFilter"
    class="org.springframework.security.web.authentication.logout.LogoutFilter">
    <constructor-arg
        value="${cas.server.address}/logout?service=${cas.server.address}" />
    <constructor-arg>
        <bean
            class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
    </constructor-arg>
    <property name="filterProcessesUrl" value="/j_spring_cas_security_logout" />
</bean>

And you should add these filters to your springSecurityFilterChain : 并且您应该将这些过滤器添加到springSecurityFilterChain

<sec:filter-chain pattern="/logout*" 
    filters="securityContextPersistenceFilter,singleLogoutFilter,casAuthenticationFilter" />
<sec:filter-chain pattern="/j_spring_cas_security_logout*"
    filters="requestSingleLogoutFilter" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM