使用 Spring Security 和 CAS 单点注销

2024-04-22

使用纯 Spring Java 配置,我在让 Spring 和 CAS 执行单点登录时遇到问题。我使用以下配置进行单点登录。我使用一个简单的 JSP 页面对 url 进行表单 POSThttps://nginx.shane.com/app/logout https://nginx.shane.com/app/logout我将 CSRF 值包含在 POST 数据中。这一切似乎都没有错误,但当我进入安全页面时,它只是让我返回而无需登录。有任何想法吗?

@Configuration
@EnableWebSecurity
public class SecurityWebAppConfig extends WebSecurityConfigurerAdapter {

@Bean
protected ServiceProperties serviceProperties() {
    ServiceProperties serviceProperties = new ServiceProperties();
    serviceProperties.setService("https://nginx.shane.com/app/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<CasAssertionAuthenticationToken> authenticationUserDetailsService() {
    return new TestCasAuthenticationUserDetailsService();
}

@Bean
public Cas20ServiceTicketValidator cas20ServiceTicketValidator() {
    return new Cas20ServiceTicketValidator("https://nginx.shane.com/cas");
}

@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("https://nginx.shane.com/cas/login");
    casAuthenticationEntryPoint.setServiceProperties(serviceProperties());

    return casAuthenticationEntryPoint;
}

@Bean
public SingleSignOutFilter singleSignOutFilter() {
    // This filter handles a Single Logout Request from the CAS Server
    return new SingleSignOutFilter();
}

@Bean
public LogoutFilter requestLogoutFilter() {
    // This filter redirects to the CAS Server to signal Single Logout should be performed
    SecurityContextLogoutHandler handler = new SecurityContextLogoutHandler();
    handler.setClearAuthentication(true);
    handler.setInvalidateHttpSession(true);

    LogoutFilter logoutFilter = new LogoutFilter("https://nginx.shane.com/", handler);
    return logoutFilter;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.addFilter(casAuthenticationFilter());
    http.addFilterBefore(requestLogoutFilter(), LogoutFilter.class);
    http.addFilterBefore(singleSignOutFilter(), CasAuthenticationFilter.class);

    http.exceptionHandling()
        .authenticationEntryPoint(casAuthenticationEntryPoint());

    http.authorizeRequests()
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
        .antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')");

    http.logout()
        .addLogoutHandler(handler)
        .deleteCookies("remove")
        .invalidateHttpSession(true)
        .logoutUrl("/logout")
        .logoutSuccessUrl("/");
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(casAuthenticationProvider());
}

}

我还添加了一个 WebListener 来处理会话销毁事件:

@WebListener
public class SecurityWebListener implements HttpSessionListener {

private SingleSignOutHttpSessionListener listener = new SingleSignOutHttpSessionListener();

@Override
public void sessionCreated(HttpSessionEvent se) {
    listener.sessionCreated(se);
}

@Override
public void sessionDestroyed(HttpSessionEvent se) {
    listener.sessionDestroyed(se);
}
}

这是日志输出

[org.springframework.security.web.FilterChainProxy] [/logout at position 6 of 14 in additional filter chain; firing Filter: 'LogoutFilter'] []
[org.springframework.security.web.util.matcher.AntPathRequestMatcher] [Checking match of request : '/logout'; against '/logout'] []
[org.springframework.security.web.authentication.logout.LogoutFilter] [Logging out user 'org.springframework.security.cas.authentication.CasAuthenticationToken@836ad34b: Principal: org.springframework.security.core.userdetails.User@586034f: Username: admin; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffdaa08: RemoteIpAddress: 127.0.0.1; SessionId: FA880C15EF09C033E1CA0C8E4785905F; Granted Authorities: ROLE_ADMIN Assertion: org.jasig.cas.client.validation.AssertionImpl@fcd38ec Credentials (Service/Proxy Ticket): ST-23-1UandqRxBcG6HCTx0Pdd-cas01.example.org' and transferring to logout destination] []
[org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler] [Invalidating session: FA880C15EF09C033E1CA0C8E4785905F] []
[org.jasig.cas.client.session.HashMapBackedSessionMappingStorage] [Attempting to remove Session=[FA880C15EF09C033E1CA0C8E4785905F]] []
[org.jasig.cas.client.session.HashMapBackedSessionMappingStorage] [Found mapping for session.  Session Removed.] []
[org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler] [Using default Url: /] []
[org.springframework.security.web.DefaultRedirectStrategy] [Redirecting to '/app/'] []

(不)幸运的是,我遇到了类似的问题;)当 CAS 尝试调用您的应用程序注销时,就会发生这种情况。一方面 CAS 尝试传递 sessionId 来执行注销,另一方面 SpringSecurity 期望获取 CSRF 令牌,而 CAS 并未发送该令牌,因为它只发送 GET 请求。 CsrfFilter 找不到 csrf 令牌并中断过滤器链。用户不知道这一点,因为 CAS 隐式调用注销请求。请求直接从 CAS 服务器发送到应用程序服务器,而不是通过在 Web 浏览器中重定向用户。

为了使其工作,您需要配置 HttpSecurity 以排除/不包含 LogoutFilter filterProcessesUrl (这是j_spring_security_logout在您的情况下,您使用默认的)。

例如,假设您想在尝试创建新管理员时检查CSRF,则需要按如下方式进行配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.addFilter(casAuthenticationFilter());
    http.addFilterBefore(requestLogoutFilter(), LogoutFilter.class);
    http.addFilterBefore(singleSignOutFilter(), CasAuthenticationFilter.class);

    http.exceptionHandling()
        .authenticationEntryPoint(casAuthenticationEntryPoint());

    http.authorizeRequests()
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
        .antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')");

    http.csrf()
        .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/admin/create"));

    http.logout()
        .addLogoutHandler(handler)
        .deleteCookies("remove")
        .invalidateHttpSession(true)
        .logoutUrl("/logout")
        .logoutSuccessUrl("/");
}

只是为了表明,我添加了

http.csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/admin/create"));.

请注意,您不能使用匹配所有模式(/admin/**),因为您可能还想调用一些 get 请求,并且 CSRF 过滤器将期望它们发送令牌。

Spring Security 3.2.x 之前的版本不会出现此类问题,因为其中引入了跨站请求伪造(CSRF)支持。

希望这些有帮助:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Spring Security 和 CAS 单点注销 的相关文章

随机推荐

  • 使用亚音速交易

    在我的网络应用程序中 我必须对用户操作进行审核 因此 每当用户执行操作时 我都会更新执行操作的对象并保留该操作的审核跟踪 现在 如果我先修改对象 然后更新审计跟踪 但审计跟踪失败了 那么怎么办 显然我需要回滚对已修改对象的更改 我可以在简单
  • 如何在 Qt 5.3 中将 QByteArray 转换为字符串?

    我正在使用一些函数来转换QVector s to QByteArray s 例如 QByteArray Serialize serialize QVector
  • 错误:MyDocument.getInitialProps()”应该解析为带有“html”属性的对象,该属性设置有有效的 html 字符串(在 document.js 中)

    现在我正在用react nodejs nextserver express制作一个迷你项目 发生意外错误 我不知道为什么 document js中会出现错误 如果您知道原因 请告诉我 谢谢 error Error MyDocument ge
  • MVVM 的 ReSharper 警告

    当我使用 WPF 实现 MVVM 模式时 我发现 ReSharper 经常警告我某些属性从未在我的 ViewModel 中使用 问题是它们正在被使用 但仅由数据绑定系统使用 有其他人遇到过这种烦恼吗 有没有办法帮助 ReSharper 意识
  • 当我从服务中调用函数时,“this”未定义

    我的电话上有这个电话component page this timerSessionService startTimer this finish bind this this onSynchronice 嗯 它与this finish 但是
  • 如何禁用Chrome中弹出窗口的位置栏和滚动条?

    对于以下代码 宽度和高度在 Chrome 中有效 但位置和滚动条不起作用 请指导 a href Click Here a 出于安全原因 Chrome 不允许您这样做 隐藏地址栏可以更轻松地假装您是某银行网站 http en wikipedi
  • 日期格式转换 %B %Y

    我们能否以某种方式转换日期 例如 November 2017 December 2017 迄今为止 我尝试导入 csv 数据 但收到了因子列 我尝试了以下代码 但没有成功 as POSIXct as character dat Date f
  • C++ 声明中的显式限定

    当第一个声明被注释掉时 以下命名空间定义无法编译 如果第一个声明foo未注释 那么它编译得很好 namespace Y void foo void Y foo 标准中的相关部分 8 3 1 说 当 declarator id 合格时 声明应
  • Azure Devops Artifacts:禁用从上游源保存包

    我有一个 npm 包的提要 其中 npmjs 设置为上游源 默认情况下 当您尝试使用其他依赖项安装上传的自定义包时 这些依赖项包会自动保存在您的源中 以节省将来安装的时间 然而 我不想要它 并希望我的提要仅托管我自己的软件包 并且每次安装时
  • Android 11 上不再支持发送短信的意图

    当以 Android 11 为目标并使用 Android 11 设备 物理设备或模拟器 时 以下方法不再适用于发送短信 更改目标 SDK 版本或设备 SDK 版本即可使其正常工作 logcat 根本没有说什么 知道为什么它不起作用吗 pri
  • Shopify 应用桥会话令牌在使用 vue.js 和 Axios 时遇到一些问题?

    我在制作 Shopify 应用程序 Vue js 作为前端和 laravel 作为后端时遇到一些问题 我正在使用 app bridge 生成会话令牌 它生成会话令牌正常 并且工作正常 直到重新生成会话令牌 重新生成会话令牌后 它会响应 会话
  • 来自 gluUnProject 的 IllegalArgumentException

    我收到此错误消息 08 30 19 20 17 774 ERROR AndroidRuntime 4681 FATAL EXCEPTION GLThread 9 08 30 19 20 17 774 ERROR AndroidRuntime
  • Swift:以编程方式导航到 ViewController 并传递数据

    我最近开始学习swift 到目前为止还不错 目前 我在尝试在视图控制器之间传递数据时遇到问题 我设法弄清楚如何使用导航控制器以编程方式在两个视图控制器之间导航 唯一的问题是现在我很难弄清楚如何将用户输入的三个字符串 对于 json api
  • 我怎样才能让两个“发展流”(分支?)相互跟踪,同时在特定方面保持不同?

    BRIEF 我想让两个 或更多 开发流 环境相互跟踪 在两个方向上发送彼此之间的更改 而不完全收敛 同时保留某些关键的 本质的差异 细节 一个具体的例子 下面是一个具体的例子 我对我的主目录 glew home 进行版本控制已有 28 年了
  • Angularjs:监听指令中的模型更改

    我试图找出当模型在指令中更新时如何监听 eventEditor directive myAmount function return restrict A link function scope elem attrs scope watch
  • opencv中以下代码行的含义是什么?

    该代码行的含义是什么以及如何将此代码转换为 javacv gray Scalar all 255 这是与此代码行相关的完整代码 Mat src imread in jpg gray cvtColor src gray CV BGR2GRAY
  • 从 iFrame 中卸载/删除内容

    是否有办法卸载已在 iframe 内加载的页面 如果可能的话 我不想将 iframe src 更改为空白页面 我基本上正在寻找可以做这样的事情的东西 frameID attr src 只是该代码似乎没有清除先前加载的页面 有没有 unloa
  • 设置 Let's encrypt with Go - 握手错误

    我正在尝试设置让我们在用 Go 编写的负载均衡器上进行加密 我尝试了自动和手动设置 但总是出错 该域正确指向我们的服务器 Digital Ocean 我什至可以从浏览器打开该网站而不会出现错误 而且 ssl 检查报告该域上没有错误 事实是
  • CSS 动画面临的问题

    我正在尝试创建一个几秒钟后会淡入视图的文本 但我遇到了问题 淡入视图工作正常 但文本在显示后几乎立即消失 其次 我需要这个动画以延迟的方式工作 但是当我设置延迟时 它似乎没有任何区别 早些时候 延迟工作得很好 为什么动画显示后不久就消失了
  • 使用 Spring Security 和 CAS 单点注销

    使用纯 Spring Java 配置 我在让 Spring 和 CAS 执行单点登录时遇到问题 我使用以下配置进行单点登录 我使用一个简单的 JSP 页面对 url 进行表单 POSThttps nginx shane com app lo