自從spring 4.0 開放以后,可以添加很多新特性的注解了。使用系統定義好的注解可以大大方便的提高開發的效率。
下面我貼一段代碼來講解注解:

通過小小的注解我們支持了以下功能:
- 使
spring.jackson.date-format屬性支持JDK8日期格式化 - 解決
request.getInputStream()一次讀取后失效痛點 - 國際化支持
- 全局跨域支持
- 接口加密/解密
- 防XSS攻擊
- 分布式限流/分布式鎖支持
我們通過自定義@EnableCorsFilter 來看一下跨域是如何支持的:
package com.battcn.boot.request.annotation;
import com.battcn.boot.request.configuration.cors.CorsFilterAutoConfiguration;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 開啟跨域支持
*
* @author Levin
* @since 2019-01-01
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({CorsFilterAutoConfiguration.class})
public @interface EnableCorsFilter {
}
@Inherited 元注解是一個標記注解,@Inherited闡述了某個被標注的類型是被繼承的。
如果一個使用了@Inherited修飾的annotation類型被用於一個class,則這個annotation將被用於該class的子類。
CorsFilterAutoConfiguration類(具體實現)
package com.battcn.boot.request.configuration.cors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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;
import static com.battcn.boot.request.utils.StringUtils.defaultString;
/**
* Cors 跨域支持
*
* @author Levin
* @since 2017/12/5 0005
*/
@Configuration
@EnableConfigurationProperties(value = {CorsFilterProperties.class})
public class CorsFilterAutoConfiguration {
private static final String PATH = "/**";
private final CorsFilterProperties properties;
@Autowired
public CorsFilterAutoConfiguration(CorsFilterProperties properties) {
this.properties = properties;
}
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin(defaultString(properties.getOrigin(), CorsConfiguration.ALL));
corsConfiguration.addAllowedHeader(defaultString(properties.getAllowedHeader(), CorsConfiguration.ALL));
corsConfiguration.addAllowedMethod(defaultString(properties.getMethod(), CorsConfiguration.ALL));
// 是否發送 Cookie 信息
corsConfiguration.setAllowCredentials(properties.getAllowCredentials());
if (properties.getMaxAge() != null) {
corsConfiguration.setMaxAge(properties.getMaxAge());
}
if (properties.getExposedHeader() != null) {
corsConfiguration.addExposedHeader(properties.getExposedHeader());
}
return corsConfiguration;
}
/**
* 跨域過濾器
*
* @return Cors過濾器
*/
@Bean
@ConditionalOnMissingBean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration(defaultString(properties.getPath(), PATH), buildConfig());
return new CorsFilter(source);
}
}
@ConditionalOnMissingBean 屬性相同,自動生成加載
@Configuration Ioc加載到bean里
@EnableConfigurationProperties 加載class配置項
@ConfigurationProperties 加載具體的配置參數
CorsFilterProperties配置類
package com.battcn.boot.request.configuration.cors;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.lang.Nullable;
/**
* Core 跨域相關配置
*
* @author Levin
* @since 2017/12/5 0005
*/
@Data
@ConfigurationProperties("request.cors")
public class CorsFilterProperties {
private Boolean enabled;
private String path;
private String origin;
private String allowedHeader;
private String method;
private String exposedHeader;
@Nullable
private Boolean allowCredentials;
@Nullable
private Long maxAge;
}
application.properties配置項

我在類屬性里定義的maxAge,但是application里面顯示的是max-age,會自動幫做轉換,如果使用maxAge屬性參數也是可以取到值的(是不是spring幫做了匹配查找)。
完成以上操作,只要在SpringApplication 啟動加上@EnableCorsFilter 就可以實現跨域了。
maven調用以下是快速使用方法:
<dependency> <groupId>com.battcn</groupId> <artifactId>request-spring-boot-starter</artifactId> <version>1.0.8-RELEASE</version> </dependency>
感謝唐亞峰提供的工具類。
