栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring security+swagger3 访问默认路径(http://localhost:端口/swagger-ui/)出现404

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring security+swagger3 访问默认路径(http://localhost:端口/swagger-ui/)出现404

spring security+swagger3 访问默认路径(http://localhost:端口/swagger-ui/)出现404
  • springboot vue3 redis MySQL spring security swagger3 前后端分离项目。
  • redis 和 rabbitMQ
    • 依赖
  • spring security
    • 依赖
  • swagger3
    • 依赖
  • 关于报错如何处理
    • swagger3 的配置
    • webmvc 的配置
    • spring security 配置
  • 配置好后,在controller加入注解,然后访问路径 http://localhost:端口/swagger-ui/index.html

springboot vue3 redis MySQL spring security swagger3 前后端分离项目。 redis 和 rabbitMQ

Redis:
Redis是一个多实用工具,是一个Key-Value的NoSQL数据库,可以作为缓存使用,开发维护很活跃,虽然它是一个Key-Value数据库存储系统,但它本身支持MQ功能,消息队列(Redis本地支持发布/订阅),应用程序中的任何短期数据,所以完全可以当做一个轻量级的队列服务来使用。

依赖
 
        
            org.springframework.session
            spring-session-data-redis
            ${spring.boot.version}
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
            ${spring.boot.version}
        

RabbitMQ:
RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。消息中间件主要用于组件之间的解耦,消息的发送者无需知道消息使用者的存在,反之亦然。

spring security

spring security 的核心功能主要包括:验证,授权,攻击防护。

依赖

        
            org.springframework.boot
            spring-boot-starter-security
            ${spring.boot.version}
        
swagger3

前后端分离的项目,接口文档的存在十分重要,swagger是一个自动生成接口文档的工具,在需求不断变更的环境下,手动编写文档的效率实在太低。与swagger2相比新版的swagger3配置更少,使用更加方便。

依赖

        
            io.springfox
            springfox-boot-starter
            3.0.0
        
关于报错如何处理

首先访问时 注意访问的是后端的端口,不要搞错。再查看是否为配置错误

swagger3 的配置

首先 swagger3 的配置正常,项目不会因为swagger3 的配置出错,所以接下来看其他的配置

package com.mindskip.xzs.configuration.spring.swagger;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.documentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
//开启 OpenApi
@EnableOpenApi
@Configuration
public class SwaggerConfig {

    public Docket docket(){
        Docket docket = new Docket(documentationType.OAS_30);
        docket.apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mindskip.xzs.controller"))
                .paths(PathSelectors.any()).build();
        return docket;
    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("作者 mxy", "作者http://www.xxx.com","example@qq.com");
        ApiInfo apiInfo = new ApiInfo(
                "Swagger3接口文档",
                "SpringBoot 整合 Swagger3 生成接口文档!",
                "1.0.0",
                "termsOfServiceUrl",
                contact,
                "这里是license",
                "这里是licenseUrl",
                new ArrayList());
        return apiInfo;
    }

}

webmvc 的配置

webmvc 和 spring security 都可以做跨域和拦截的配置,所以注意我加的注释部分,对swagger3 资源进行放行。

package com.mindskip.xzs.configuration.spring.mvc;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");

        registry.addRedirectViewController("/", "/student/index.html");
        registry.addRedirectViewController("/student", "/student/index.html");
        registry.addRedirectViewController("/admin", "/admin/index.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("
        registry.addResourceHandler("/swagger-ui

@Configuration
@EnableWebSecurity
public class SecurityConfigurer {

    @Configuration
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        private final SystemConfig systemConfig;
        private final LoginAuthenticationEntryPoint restAuthenticationEntryPoint;
        private final RestAuthenticationProvider restAuthenticationProvider;
        private final RestDetailsServiceImpl formDetailsService;
        private final RestAuthenticationSuccessHandler restAuthenticationSuccessHandler;
        private final RestAuthenticationFailureHandler restAuthenticationFailureHandler;
        private final RestLogoutSuccessHandler restLogoutSuccessHandler;
        private final RestAccessDeniedHandler restAccessDeniedHandler;

        @Autowired
        public FormLoginWebSecurityConfigurerAdapter(SystemConfig systemConfig, LoginAuthenticationEntryPoint restAuthenticationEntryPoint, RestAuthenticationProvider restAuthenticationProvider, RestDetailsServiceImpl formDetailsService, RestAuthenticationSuccessHandler restAuthenticationSuccessHandler, RestAuthenticationFailureHandler restAuthenticationFailureHandler, RestLogoutSuccessHandler restLogoutSuccessHandler, RestAccessDeniedHandler restAccessDeniedHandler) {
            this.systemConfig = systemConfig;
            this.restAuthenticationEntryPoint = restAuthenticationEntryPoint;
            this.restAuthenticationProvider = restAuthenticationProvider;
            this.formDetailsService = formDetailsService;
            this.restAuthenticationSuccessHandler = restAuthenticationSuccessHandler;
            this.restAuthenticationFailureHandler = restAuthenticationFailureHandler;
            this.restLogoutSuccessHandler = restLogoutSuccessHandler;
            this.restAccessDeniedHandler = restAccessDeniedHandler;
        }

        
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.headers().frameOptions().disable();

            List securityIgnoreUrls = systemConfig.getSecurityIgnoreUrls();
            String[] ignores = new String[securityIgnoreUrls.size()];
            
            http
                    .addFilterAt(authenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                    .exceptionHandling().authenticationEntryPoint(restAuthenticationEntryPoint)
                    .and().authenticationProvider(restAuthenticationProvider)
                    .authorizeRequests()
                    .antMatchers(securityIgnoreUrls.toArray(ignores)).permitAll()
                    .antMatchers("/api/admin
        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/swagger-ui/**", "/swagger-resources/**", "/v3/**");

        }

        @Bean
        public CorsConfigurationSource corsConfigurationSource() {
            
            final CorsConfiguration configuration = new CorsConfiguration();
            configuration.setMaxAge(3600L);
           
            configuration.setAllowedOrigins(Collections.singletonList("*"));
            configuration.setAllowedMethods(Collections.singletonList("*"));
            configuration.setAllowCredentials(true);
            configuration.setAllowedHeaders(Collections.singletonList("*")); 
            final UrlbasedCorsConfigurationSource source = new UrlbasedCorsConfigurationSource();
            source.registerCorsConfiguration("/api/**", configuration);
          
            return source;
        }


        @Bean
        public RestLoginAuthenticationFilter authenticationFilter() throws Exception {
            RestLoginAuthenticationFilter authenticationFilter = new RestLoginAuthenticationFilter();
            authenticationFilter.setAuthenticationSuccessHandler(restAuthenticationSuccessHandler);
            authenticationFilter.setAuthenticationFailureHandler(restAuthenticationFailureHandler);
            authenticationFilter.setAuthenticationManager(authenticationManagerBean());
            authenticationFilter.setUserDetailsService(formDetailsService);
            return authenticationFilter;
        }


    }
}

配置好后,在controller加入注解,然后访问路径 http://localhost:端口/swagger-ui/index.html

@Api:用在请求的类上,表示对类的说明

tags="说明该类的作用,可以在UI界面上看到的注解"
value="该参数没什么意义,在UI界面上也看到,所以不需要配置"

@ApiOperation:用在请求的方法上,说明方法的用途、作用

value="说明方法的用途、作用"
notes="方法的备注说明"

@ApiImplicitParams:用在请求的方法上,表示一组参数说明

@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
    name:参数名
    value:参数的汉字说明、解释
    required:参数是否必须传
    paramType:参数放在哪个地方
        · header --> 请求参数的获取:@RequestHeader
        · query --> 请求参数的获取:@RequestParam
        · path(用于restful接口)--> 请求参数的获取:@PathVariable
        · div(不常用)
        · form(不常用)   
    dataType:参数类型,默认String,其它值dataType="Integer"     
    defaultValue:参数的默认值

@ApiResponses:用在请求的方法上,表示一组响应

@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
    code:数字,例如400
    message:信息,例如"请求参数没填好"
    response:抛出异常的类

@ApiModel:用于响应类上,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述响应类的属性

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572757.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号