Java实战:SpringBoot集成单点登录CAS
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 配置CAS服务器的URL、服务登录URL以及服务验证URL
auth.
casAuthentication()
.casServerUrl("http://cas.example.org/cas")
.serviceProperties(serviceProperties ->
serviceProperties.service("http://localhost:8080/login/cas")
);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(casAuthenticationEntryPoint())
.and()
.logout()
.logoutUrl("http://cas.example.org/cas/logout")
.logoutSuccessUrl("http://localhost:8080/")
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and()
.addFilter(casAuthenticationFilter());
}
// 省略CASAuthenticationFilter和CASAuthenticationEntryPoint的定义
}
这个代码示例展示了如何在Spring Boot应用中配置CAS单点登录。它定义了一个SecurityConfig
类,继承自WebSecurityConfigurerAdapter
,并重写了configure
方法来设置CAS服务器的URL、服务登录URL以及服务验证URL。同时,它配置了HTTP安全策略,包括禁用CSRF保护、设置会话管理策略为SessionCreationPolicy.NEVER
,并添加了CAS认证过滤器。
评论已关闭