CrosConfig
这段代码是一个跨域配置类 `CrosConfig`,它使用了 Spring Framework 提供的 `CorsFilter` 来处理跨域请求。让我为你解释一下:
### 类:`CrosConfig`
- **包**:`com.cqupt.software_1.config`
- 该类位于 `com.cqupt.software_1` 包中的 `config` 子包下。
### 注解:
- **`@Configuration`**:
- 说明这是一个配置类,Spring 在启动时会加载它。
### 方法:
- **`corsFilter()`**:
- 功能:配置 CORS 过滤器。
- 返回类型:`CorsFilter`
- 作用:创建一个 `CorsFilter` 实例来处理跨域请求。
### 实现步骤:
1. **创建 CORS 配置信息**:
- 创建 `CorsConfiguration` 实例 `config`,配置允许的原始域、是否发送 Cookie、允许的请求方式、允许的请求头部信息以及需要暴露的头部信息。
2. **添加映射路径**:
- 创建 `UrlBasedCorsConfigurationSource` 实例 `corsConfigurationSource`,并注册上述创建的 `CorsConfiguration` 实例 `config` 到路径 `"/**"` 下。
3. **返回 `CorsFilter` 实例**:
- 将 `corsConfigurationSource` 作为参数创建 `CorsFilter` 实例并返回,从而将 CORS 配置应用到过滤器中。
### 目的:
- **功能**:
- 处理跨域请求,允许不同源的请求访问服务器资源,提高系统的可访问性。
- 配置跨域策略,确保系统在安全性和可用性之间取得平衡。
### 概要:
`CrosConfig` 类提供了跨域请求的配置,通过添加 CORS 配置信息并将其应用到 `CorsFilter` 中,实现了跨域请求的处理。这样就能够在 Web 应用中处理来自不同源的请求,保障系统的安全性和可用性。
package com.cqupt.software_1.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CrosConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOriginPattern("*");
config.setAllowCredentials(true);
config.addAllowedMethod("*");
config.addAllowedHeader("*");
config.addExposedHeader("*");
UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
corsConfigurationSource.registerCorsConfiguration("/**", config);
return new CorsFilter(corsConfigurationSource);
}
}
RemoteConfig
这段代码定义了一个名为 `RemoteConfig` 的组件类,用于读取远程配置信息,并提供了对应的属性和注解。让我解释一下:
### 类:`RemoteConfig`
- **包**:`com.cqupt.software_1.config`
- 该类位于 `com.cqupt.software_1` 包中的 `config` 子包下。
### 注解:
- **`@Component`**:
- 表明这是一个 Spring 组件类,会被 Spring 容器自动扫描并加载。
- **`@ConfigurationProperties(prefix = "remote")`**:
- 指定了配置属性的前缀为 `"remote"`,表示从配置文件中读取以 `"remote"` 开头的配置项。
- **`@Data`**:
- Lombok 提供的注解,自动生成 Getter、Setter、`equals()`、`hashCode()` 和 `toString()` 方法。
### 属性:
- **`remoteHost`**:
- 类型:`String`
- 功能:远程主机地址。
- **`remotePort`**:
- 类型:`int`
- 功能:远程主机端口。
- **`remoteUsername`**:
- 类型:`String`
- 功能:远程主机用户名。
- **`remotePassword`**:
- 类型:`String`
- 功能:远程主机密码。
- **`remoteFilePath`**:
- 类型:`String`
- 功能:远程文件路径。
### 目的:
- **功能**:
- 用于读取远程配置信息,包括主机地址、端口、用户名、密码以及文件路径等。
- 将这些配置信息注入到 Spring 容器中,方便在其他组件中使用。
### 概要:
`RemoteConfig` 类是一个 Spring 组件,通过 `@ConfigurationProperties` 注解从配置文件中读取远程配置信息,并提供了对应的属性。这样可以在应用程序中方便地使用这些配置信息,比如连接远程服务器或者访问远程文件等。
package com.cqupt.software_1.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "remote")
@Data
public class RemoteConfig {
private String remoteHost;
private int remotePort;
private String remoteUsername;
private String remotePassword;
private String remoteFilePath;
}
WebMVCConfig
这段代码是一个 Spring MVC 的配置类 `WebMVCConfig`,它实现了 `WebMvcConfigurer` 接口,用于配置拦截器。让我为你解释一下:
### 类:`WebMVCConfig`
- **包**:`com.cqupt.software_1.config`
- 该类位于 `com.cqupt.software_1` 包中的 `config` 子包下。
### 注解:
- **`@Configuration`**:
- 说明这是一个配置类,Spring 在启动时会加载它。
### 实现接口:
- **`WebMvcConfigurer`**:
- Spring MVC 提供的配置接口,用于配置 Spring MVC 的行为。
### 属性:
- **`UserLoginInterceptor userLoginInterceptor`**:
- 类型:`UserLoginInterceptor`
- 功能:用户登录拦截器,用于拦截未登录用户的请求。
### 方法:
- **`addInterceptors(InterceptorRegistry registry)`**:
- 功能:配置拦截器。
- 参数:`InterceptorRegistry registry`,拦截器注册表。
- 作用:将 `userLoginInterceptor` 添加到拦截器注册表中,并配置需要拦截的路径和排除的路径。
### 实现步骤:
- **添加拦截器**:
- 调用 `registry.addInterceptor()` 方法添加 `userLoginInterceptor` 拦截器。
- **配置拦截路径**:
- 使用 `addPathPatterns()` 方法配置需要拦截的路径为 `"/**"`,即所有路径。
- **配置排除路径**:
- 使用 `excludePathPatterns()` 方法配置不需要拦截的路径,如 `"/user/login"`、`"/user/signUp"` 和 `"/user/logout"`。
### 目的:
- **功能**:
- 配置 Spring MVC 的拦截器,实现对请求的拦截和处理。
- 在拦截器中添加用户登录拦截器,用于验证用户是否登录,保护需要登录才能访问的资源。
### 概要:
`WebMVCConfig` 类是一个 Spring MVC 的配置类,通过实现 `WebMvcConfigurer` 接口,配置了拦截器用于对请求进行拦截和处理。其中,添加了用户登录拦截器,并配置了需要拦截和排除拦截的路径,以实现对用户登录状态的检查和保护。
package com.cqupt.software_1.config;
import com.cqupt.software_1.intercepter.UserLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Autowired
UserLoginInterceptor userLoginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(userLoginInterceptor).addPathPatterns("/**").excludePathPatterns("/user/login").excludePathPatterns("/user/signUp").excludePathPatterns("/user/logout");
}
}