添加小编微信 372787553 ,带您进入程序员交流群!
添加Swagger 配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
ParameterBuilder ticketPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
ticketPar.name("Authorization").description("user ticket")
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build();
pars.add(ticketPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//加了ApiOperation注解的类,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//包下的类,才生成接口文档
//.apis(basePackage("com.bmw.pandora.digtal.lrp.**"))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars);
//.securitySchemes(security());
}
/**
* Predicate that matches RequestHandler with given base package name for the class of the handler method.
* This predicate includes all request handlers matching the provided basePackage
*
* @param basePackage - base package of the classes
* @return this
*/
public static Predicate<RequestHandler> basePackage(final String basePackage) {
return input -> declaringClass(Objects.requireNonNull(input)).map(handlerPackage(basePackage)).orElse(true);
}
/**
* 处理包路径配置规则,支持多路径扫描匹配以逗号隔开
*
* @param basePackage 扫描包路径
* @return Function
*/
private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
return input -> {
for (String strPackage : basePackage.split(",")) {
boolean isMatch = Objects.requireNonNull(input).getPackage().getName().startsWith(strPackage);
if (isMatch) {
return true;
}
}
return false;
};
}
/**
* @param input RequestHandler
* @return Optional
*/
private static Optional<Class<?>> declaringClass(RequestHandler input) {
return Optional.ofNullable(input.declaringClass());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Java 有货")
.description("API文档")
.termsOfServiceUrl("------")
.version("1.0.0")
.build();
}
}
权限连接配置
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
private final DataSource dataSource;
public ResourceServerConfiguration(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
/**
* 声明 ClientDetails实现
*
* @return ClientDetailsService
*/
@Bean
public ClientDetailsService clientDetailsService() {
return new JdbcClientDetailsService(dataSource);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and()
.authorizeRequests()
//放行路径
.antMatchers("/v2/api-docs",
"/swagger-resources/configuration/ui",
"/swagger-resources",
"/swagger-resources/configuration/security",
"/swagger-ui.html",
"/course/coursebase/**").permitAll()
// 指定监控访问权限
//.requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling()
// 认证鉴权错误处理,为了统一异常处理。每个资源服务器都应该加上。
.accessDeniedHandler(new BaseAccessDeniedHandler())
.authenticationEntryPoint(new BaseAuthenticationEntryPoint())
//配置跨域
.and().cors()
.and().csrf().disable()
// 禁用httpBasic
.httpBasic().disable();
}
}
配置完成访问项目地址http://localhost:8096/swagger-ui.htm
在Authorization 添加token即可进行服务接口调用
添加小编微信 372787553 ,带您进入程序员交流群!