From 18f9c27e17a4bef2f1d6571bf113b9bb741046af Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期一, 09 十一月 2020 15:09:31 +0800 Subject: [PATCH] 添加接口生成工具Swagger --- consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/controller/TestController.java | 5 + consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/config/WebSecurityConfig.java | 89 +++++++++++++++++++++++++++++ consumer-jdGiftCoupon/pom.xml | 15 +++++ pom.xml | 8 ++ consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/config/Swagger2Config.java | 34 +++++++++++ consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/query/TestQuery.java | 10 +++ 6 files changed, 160 insertions(+), 1 deletions(-) diff --git a/consumer-jdGiftCoupon/pom.xml b/consumer-jdGiftCoupon/pom.xml index 96ee296..90d72a5 100644 --- a/consumer-jdGiftCoupon/pom.xml +++ b/consumer-jdGiftCoupon/pom.xml @@ -36,6 +36,21 @@ </exclusion> </exclusions> </dependency> + + <dependency> + <groupId>io.springfox</groupId> + <artifactId>springfox-swagger2</artifactId> + <version>2.9.2</version> + </dependency> + + + <dependency> + <groupId>io.springfox</groupId> + <artifactId>springfox-swagger-ui</artifactId> + <version>2.9.2</version> + </dependency> + + </dependencies> <build> diff --git a/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/config/Swagger2Config.java b/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/config/Swagger2Config.java new file mode 100644 index 0000000..ae8611b --- /dev/null +++ b/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/config/Swagger2Config.java @@ -0,0 +1,34 @@ +package com.ks.consumerjdgiftcoupon.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class Swagger2Config { + @Bean + Docket docket() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.basePackage("com.ks.consumerjdgiftcoupon.controller")) + .paths(PathSelectors.any()) + .build().apiInfo(new ApiInfoBuilder() + //缃戠珯鎻忚堪 + .description("鎺ュ彛鏂囨。鐨勬弿杩颁俊鎭�") + .title("寰汉浜嬮」鐩帴鍙f枃妗�") + //鑱旂郴浜轰俊鎭� + .contact(new Contact("yolo", "blog.csdn.net", "xxxx@gmail.com")) + //鐗堟湰 + .version("v1.0") + .license("Apache2.0") + .build()); + } + +} diff --git a/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/config/WebSecurityConfig.java b/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/config/WebSecurityConfig.java new file mode 100644 index 0000000..d826285 --- /dev/null +++ b/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/config/WebSecurityConfig.java @@ -0,0 +1,89 @@ +package com.ks.consumerjdgiftcoupon.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + class CustomUserService implements UserDetailsService { + + @Override + public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { + //閫氳繃鍚嶇О鏌ヨ鐢ㄦ埛 + + return null; + } + } + + class CustomAuth implements AuthenticationProvider { + + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + authentication.setAuthenticated(true); + return authentication; + } + + @Override + public boolean supports(Class<?> aClass) { + return true; + } + } + + + @Bean + UserDetailsService customUserService() { + return new CustomUserService(); + } + + @Bean + AuthenticationProvider customAuth() { + return new CustomAuth(); + } + + /** + * 瀹氫箟鐢ㄦ埛璁よ瘉瑙勫垯 + * + * @param auth + * @throws Exception + */ + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(customUserService()); + auth.authenticationProvider(customAuth()); + } + + /** + * 瀹氫箟鎺堟潈瑙勫垯 + * + * @param http + * @throws Exception + */ + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/", "/home").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() +// .loginPage("/login") + .permitAll() + .and() + .logout() + .permitAll(); + } + + +} diff --git a/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/controller/TestController.java b/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/controller/TestController.java index f959cea..84fed28 100644 --- a/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/controller/TestController.java +++ b/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/controller/TestController.java @@ -1,6 +1,8 @@ package com.ks.consumerjdgiftcoupon.controller; import com.ks.consumerjdgiftcoupon.query.TestQuery; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; @@ -8,10 +10,11 @@ import javax.validation.Valid; +@Api(tags = "娴嬭瘯鏁版嵁鎺ュ彛") @Controller @RequestMapping("test") public class TestController { - + @ApiOperation(value = "娴嬭瘯", notes = "娴嬭瘯瀛楁楠岃瘉") @RequestMapping("valid") @ResponseBody public String valid(@Valid TestQuery query, BindingResult bindingResult) { diff --git a/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/query/TestQuery.java b/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/query/TestQuery.java index 3afb795..b49cc1a 100644 --- a/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/query/TestQuery.java +++ b/consumer-jdGiftCoupon/src/main/java/com/ks/consumerjdgiftcoupon/query/TestQuery.java @@ -1,9 +1,13 @@ package com.ks.consumerjdgiftcoupon.query; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + import javax.validation.constraints.*; import java.math.BigDecimal; import java.util.Date; +@ApiModel(value = "娴嬭瘯鏌ヨ",description = "娴嬭瘯鏌ヨ") public class TestQuery { // @AssertFalse(message = "鎵�娉ㄨВ鐨勫厓绱犲繀椤绘槸Boolean绫诲瀷锛屼笖鍊间负false") @@ -14,24 +18,30 @@ // @Future(message = "鏃ユ湡鏄惁鍦ㄥ綋鍓嶆椂闂翠箣鍚�") + @ApiModelProperty(value = "鏄电О") @NotEmpty(message = "娉ㄥ唽澶辫触锛屽悕瀛椾笉鑳戒负绌�") private String name; + @ApiModelProperty(value = "骞撮緞") @Max(value = 19, message = "璇ュ瓧娈垫渶澶у�间负19") @Min(value = 0, message = "璇ュ瓧娈垫渶灏忓�间负0") private int age; + @ApiModelProperty(value = "韬珮") @DecimalMax(value = "3.00", message = "鎵�娉ㄨВ鐨勫厓绱犲繀椤绘槸鏁板瓧锛屼笖鍊煎皬浜庣瓑浜庣粰瀹氱殑鍊�") @DecimalMin(value = "0.00", message = "鎵�娉ㄨВ鐨勫厓绱犲繀椤绘槸鏁板瓧锛屼笖鍊煎ぇ浜庣瓑浜庣粰瀹氱殑鍊�") private BigDecimal height; + @ApiModelProperty(value = "鍑虹敓鏃ユ湡") @Past(message = "鍑虹敓鏃ユ湡涓嶈兘澶т簬褰撳墠鏃ユ湡") private Date birthday; + @ApiModelProperty(value = "閭") @Email(message = "閭欢鏍煎紡鏈夎") private String email;//閭欢 + @ApiModelProperty(value = "绛惧悕") @Pattern(regexp = "[abc]", message = "姝e垯涓嶅尮閰�") private String sign; diff --git a/pom.xml b/pom.xml index a325bc1..fafc0be 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,12 @@ <artifactId>spring-boot-starter-web</artifactId> </dependency> + <!--瀹夊叏妗嗘灦 --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-security</artifactId> + </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> @@ -46,6 +52,8 @@ <artifactId>spring-boot-starter-validation</artifactId> </dependency> + + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> -- Gitblit v1.8.0