前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot Web 自定义注解篇(注解很简单很好用)

Spring Boot Web 自定义注解篇(注解很简单很好用)

作者头像
爱撸猫的杰
发布2019-04-01 11:30:35
8830
发布2019-04-01 11:30:35
举报
文章被收录于专栏:爱撸猫的杰

自从spring 4.0 开放以后,可以添加很多新特性的注解了。使用系统定义好的注解可以大大方便的提高开发的效率。

下面我贴一段代码来讲解注解:

通过小小的注解我们支持了以下功能:

  • 使 spring.jackson.date-format 属性支持 JDK8 日期格式化
  • 解决 request.getInputStream() 一次读取后失效痛点
  • 国际化支持
  • 全局跨域支持
  • 接口加密/解密
  • 防XSS攻击
  • 分布式限流/分布式锁支持

我们通过自定义@EnableCorsFilter 来看一下跨域是如何支持的:

代码语言:javascript
复制
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的子类。 

代码语言:javascript
复制
CorsFilterAutoConfiguration类(具体实现)
代码语言:javascript
复制
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);
    }

}
代码语言:javascript
复制
@ConditionalOnMissingBean 属性相同,自动生成加载

@Configuration   Ioc加载到bean里

@EnableConfigurationProperties   加载class配置项
代码语言:javascript
复制
@ConfigurationProperties  加载具体的配置参数


CorsFilterProperties配置类
代码语言:javascript
复制
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调用以下是快速使用方法:

代码语言:javascript
复制
<dependency>
    <groupId>com.battcn</groupId>
    <artifactId>request-spring-boot-starter</artifactId>
    <version>1.0.8-RELEASE</version>
</dependency>

感谢唐亚峰提供的工具类。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档