spring cloud 前后端分离集成CAS client
在Spring Cloud前后端分离的项目中集成CAS(Central Authentication Service)客户端,可以通过Spring Security与CAS集成来实现单点登录。以下是一个简化的例子:
- 添加依赖到
pom.xml
:
<dependencies>
<!-- Spring Security CAS Dependency -->
<dependency>
<groupId>org.springframework.security</groupId>org.springframework.security.securitycas</artifactId>
<version>${spring.security.cas.version}</version>
</dependency>
<!-- Spring Security Config -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
- 配置
application.properties
或application.yml
:
# CAS Server的URL
cas.server-url-prefix=http://cas.example.org/cas
# CAS服务登录成功后的URL
cas.server-login-url=http://cas.example.org/cas/login
# 应用的登出URL
cas.client-host-url=http://client.example.org
- 配置CAS客户端:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.cas.web.CasAuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
@Configuration
public class CasSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(casAuthenticationEntryPoint())
.and()
.logout()
.logoutUrl("/logout")
.addLogoutHandler(new SecurityContextLogoutHandler())
.addLogoutHandler(casLogoutHandler())
.and()
.addFilter(casAuthenticationFilter())
.addFilterAfter(casLogoutFilter(), LogoutFilter.class);
}
private CasAuthenticationEntryPoint casAuthenticationEntryPoint() {
CasAuthenticationEntryPoint entryPoint = new CasAuthenticationEntryPoint();
entryPoint.setLoginUrl("http://cas.example.org/cas/login");
entryPoint.setServiceProperties(serviceProperties())
评论已关闭