softwate1_config模块

这里写目录标题

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;

/**
 * 跨域处理
 * @author  yangxing
 * @since 2023/5/22
 */
@Configuration
public class CrosConfig {

    @Bean
    public CorsFilter corsFilter() {
        //1. 添加 CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //放行哪些原始域
        config.addAllowedOriginPattern("*");
//        config.addAllowedOrigin("http://10.16.80.16:8080");
        //是否发送 Cookie
        config.setAllowCredentials(true);
        //放行哪些请求方式
        config.addAllowedMethod("*");
        //放行哪些原始请求头部信息
        config.addAllowedHeader("*");
        //暴露哪些头部信息
        config.addExposedHeader("*");
        //2. 添加映射路径
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", config);
        //3. 返回新的CorsFilter
        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");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值