首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹簧靴安全大摇大摆跳起

弹簧靴安全大摇大摆跳起
EN

Stack Overflow用户
提问于 2021-01-04 04:49:35
回答 1查看 860关注 0票数 1

pom.xml

代码语言:javascript
复制
<!-- swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

spring-security配置

代码语言:javascript
复制
protected void configure(HttpSecurity httpSecurity) throws Exception {

        String[] SWAGGERS = {
                "/swagger/**",
                "/v3/**"
        };

        httpSecurity
                .authorizeRequests(expressionInterceptUrlRegistry ->
                        expressionInterceptUrlRegistry
                                // 放行 druid 页面
                                .antMatchers("/zhy-druid/**").permitAll()
                                .antMatchers(SWAGGERS).anonymous()
                                .anyRequest().authenticated()
                );

        httpSecurity
                .formLogin(httpSecurityFormLoginConfigurer ->
                        httpSecurityFormLoginConfigurer
                                .loginPage("/authentication")
                                .successHandler(accountAuthenticationSuccessHandler)
                                .failureHandler(accountAuthenticationFailureHandler)
                );

        httpSecurity
                .logout(httpSecurityLogoutConfigurer ->
                        httpSecurityLogoutConfigurer
                                .logoutUrl("/cancellation")
                                .logoutSuccessHandler(accountLogoutSuccessHandler)
                );

        httpSecurity
                .exceptionHandling(httpSecurityExceptionHandlingConfigurer ->
                        httpSecurityExceptionHandlingConfigurer
                                .authenticationEntryPoint(accountAuthenticationEntryPointHandler)
                                .accessDeniedHandler(accountAccessDeniedHandler)
                );

        httpSecurity
                .cors();

        httpSecurity
                .csrf()
                .disable();
    }

application-local.yml

代码语言:javascript
复制
springfox:
  documentation:
    enabled: true
    swagger-ui:
      base-url: /swagger

我得到了这个结果。

无法呈现此定义,所提供的定义没有指定有效的版本字段。

请指定有效的SwaggerOpenAPI版本字段。支持的版本字段是swagger: 2.0,与openapi: 3.0.n匹配的字段(例如openapi: 3.0.0)。

EN

回答 1

Stack Overflow用户

发布于 2021-01-04 05:56:50

Openapi是最新的库,推荐用于spring引导应用程序。这是下一个版本的“傲慢”。

在应用程序中添加以下工作openapi代码。

pom.xml

代码语言:javascript
复制
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.2.32</version>
</dependency>

Spring-Security配置允许开放的api url。

代码语言:javascript
复制
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
    @Autowired
    private UserServiceImpl userServiceImpl;
    
    @Bean
    BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .authorizeRequests()
                    .antMatchers(SecurityConstants.SIGN_UP_URL).permitAll()
                    .antMatchers("/swagger-ui/**").permitAll()
                    .antMatchers("/v3/**").permitAll()
                    .antMatchers("/api-docs.html").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilter(new AuthorizationFilter(authenticationManager()))
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }
}

application.yml

代码语言:javascript
复制
springdoc:
  swagger-ui.path: /api-docs.html
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65557795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档