Springboot学习笔记之springboot+shiro+cas
时间: 2024-01-02 22:02:22 浏览: 216
idea开发工具SpringBoot+Mybaits Plus+Lombok+Shiro集成(一)
Spring Boot 是一个快速开发框架,它提供了一系列的工具和插件,可以快速构建一个企业级的应用程序。而 Shiro 是一个强大而灵活的安全框架,可以提供身份验证、授权、密码加密、会话管理等功能。CAS 是一个单点登录(SSO)协议,可以实现用户在多个应用系统中使用同一个身份验证。
下面是一个简单的 Spring Boot + Shiro + CAS 的示例应用程序:
1. 创建一个 Spring Boot 应用程序,并添加依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.6.0</version>
</dependency>
```
2. 配置 Shiro:
```java
@Configuration
public class ShiroConfig {
@Bean
public CasRealm casRealm() {
CasRealm realm = new CasRealm();
realm.setCasServerUrlPrefix("https://cas.example.com/cas");
realm.setCasService("https://myapp.example.com/cas");
realm.setDefaultRoles("user");
realm.setRoleAttributeNames("memberOf");
return realm;
}
@Bean
public DefaultWebSecurityManager securityManager() {
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
manager.setRealm(casRealm());
return manager;
}
@Bean
public ShiroFilterFactoryBean shiroFilter() {
ShiroFilterFactoryBean filter = new ShiroFilterFactoryBean();
filter.setSecurityManager(securityManager());
filter.setLoginUrl("https://cas.example.com/cas/login?service=https://myapp.example.com/cas");
filter.setSuccessUrl("/home");
filter.setUnauthorizedUrl("/403");
filter.setFilterChainDefinitionMap(Collections.singletonMap("/**", "authc"));
return filter;
}
}
```
3. 配置 CAS:
```java
@Configuration
public class CasConfig {
@Bean
public CasAuthenticationFilter casAuthenticationFilter() {
CasAuthenticationFilter filter = new CasAuthenticationFilter();
filter.setCasServerLoginUrl("https://cas.example.com/cas/login");
filter.setServerName("https://myapp.example.com/cas");
filter.setAuthenticationSuccessHandler(authenticationSuccessHandler());
filter.setAuthenticationFailureHandler(authenticationFailureHandler());
return filter;
}
@Bean
public SimpleUrlAuthenticationSuccessHandler authenticationSuccessHandler() {
SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler();
handler.setDefaultTargetUrl("/home");
handler.setAlwaysUseDefaultTargetUrl(true);
return handler;
}
@Bean
public SimpleUrlAuthenticationFailureHandler authenticationFailureHandler() {
SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler();
handler.setDefaultFailureUrl("/login?error=true");
return handler;
}
@Bean
public FilterRegistrationBean<CasAuthenticationFilter> casFilterRegistrationBean() {
FilterRegistrationBean<CasAuthenticationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(casAuthenticationFilter());
registration.addUrlPatterns("/*");
registration.setName("CAS Authentication Filter");
registration.setOrder(1);
return registration;
}
@Bean
public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> singleSignOutHttpSessionListener() {
ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> registration = new ServletListenerRegistrationBean<>();
registration.setListener(new SingleSignOutHttpSessionListener());
registration.setOrder(2);
return registration;
}
@Bean
public ServletRegistrationBean<Servlet> casValidationServletRegistrationBean() {
ServletRegistrationBean<Servlet> registration = new ServletRegistrationBean<>();
registration.setServlet(new Cas20ProxyReceivingTicketValidationFilter());
registration.addUrlMappings("/cas/*");
registration.setName("CAS Validation Filter");
registration.setOrder(3);
return registration;
}
}
```
4. 创建一个 HomeController:
```java
@Controller
public class HomeController {
@GetMapping("/home")
public String home() {
return "home";
}
@GetMapping("/403")
public String error403() {
return "403";
}
}
```
5. 创建一个 CAS 认证服务器:
```java
@SpringBootApplication
public class CasServerApplication {
public static void main(String[] args) {
SpringApplication.run(CasServerApplication.class, args);
}
}
```
6. 创建一个 CAS 客户端:
```java
@SpringBootApplication
@EnableScheduling
public class CasClientApplication {
public static void main(String[] args) {
SpringApplication.run(CasClientApplication.class, args);
}
}
```
这就是一个简单的 Spring Boot + Shiro + CAS 的示例应用程序。
阅读全文