当前位置: 首页 > 知识库问答 >
问题:

Swagger- /api-docs的自定义身份验证

邵伟
2023-03-14

我们有一个Spring5,非SpringBoot应用程序,使用Springfox 2.9.2 Swagger UI。

我不知道如何保护 /api-docsendpoint:我希望它每次访问时都调用我的身份验证函数。我让它为swagger-ui.html工作,但没有成功 /api-docs.这是我得到的。

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {

    @Autowired
    protected AuthService authService;


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        // registry.addViewController("/docs/swagger/api-docs"); doesnt work
        registry.addRedirectViewController("/docs/swagger/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui");
        registry.addRedirectViewController("/docs/swagger/swagger-resources/configuration/security", "/swagger-resources/configuration/security");
        registry.addRedirectViewController("/docs/swagger/swagger-resources", "/swagger-resources");
    }

    class Interceptor implements HandlerInterceptor{
        @Override
        public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler ) {
            try{
                authService.assertAdmin(); // I need to call this
            }catch (Exception e){
                return false;
            }
            return true;
        }
    }

    @Override
    public void addInterceptors( final InterceptorRegistry registry) {
        registry.addInterceptor(new Interceptor());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // docs/swagger/index.html
        registry.addResourceHandler("/docs/swagger/swagger-ui.html**")
            .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");

        // docs/swagger/webjars
        registry.addResourceHandler("/docs/swagger/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

另一种选择是永久关闭对 /api-docs 的访问,只需直接调用从某个新终结点生成 JSON 的方法。这可能吗?

共有1个答案

傅浩漫
2023-03-14

最终,我按照@UsamaAmjad的建议,通过Spring安保解决了这个问题。

open class SecurityInitializer : AbstractSecurityWebApplicationInitializer()

@Configuration
@EnableWebSecurity
open class SecurityConfig : WebSecurityConfigurerAdapter() {


    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
        http.antMatcher("/docs/swagger/v2/api-docs").addFilter(myFilter())
    }

    open fun myFilter() = object : FilterSecurityInterceptor() {
        override fun doFilter(request: ServletRequest?, response: ServletResponse?, chain: FilterChain?) {

            if (do your stuff here) {
                chain!!.doFilter(request,response) // continue with other filters
            } else {
                super.doFilter(request, response, chain) // filter this request
            }
        }
    }
}
 类似资料:
  • 我正在将swagger应用于我的一个项目。 然后,他们应该在标头中随每次调用一起发送此令牌。 所以它就像: 看看swagger文档,它似乎只是为了处理OAuth而设置的。有没有办法让swagger以我的方式处理身份验证?我的意思是在生成的代码中。我可以为项目生成代码,它只有“Basic:...”作为选项。

  • 问题内容: 我正在为Angular 5应用程序创建API。我想使用JWT进行身份验证。 我想使用Spring Security提供的功能,以便我可以轻松地使用角色。 我设法禁用基本身份验证。但是使用时我仍然会收到登录提示。 我只想输入403而不是提示。因此,通过检查令牌标题的“事物”(是否是过滤器?)来覆盖登录提示。 我只想在将返回JWT令牌的控制器中进行登录。但是我应该使用哪种Spring Se

  • 我正在为Angular 5应用程序创建API。我希望使用JWT进行身份验证。 我希望使用spring security提供的特性,以便能够轻松地处理角色。 我只想在返回JWT令牌的控制器中进行的登录。但是我应该使用什么Spring Securitybean来检查用户凭据呢?我可以构建自己的服务和存储库,但我希望尽可能使用spring security提供的特性。 这个问题的简短版本只是: 我如何自

  • 问题内容: 这是我的情况: 一个Web应用程序对许多应用程序执行某种SSO 登录的用户,而不是单击链接,该应用就会向正确的应用发布包含用户信息(名称,pwd [无用],角色)的帖子 我正在其中一个应用程序上实现SpringSecurity以从其功能中受益(会话中的权限,其类提供的方法等) 因此,我需要开发一个 自定义过滤器 -我猜想-能够从请求中检索用户信息,通过自定义 DetailsUserSe

  • 问题内容: 我可以使用Google帐户在AppEngine中对用户进行身份验证的方式非常好。 但是,我需要使用 自定义的身份验证登录系统 。 我将有一个AppUsers表,其中包含用户名和加密密码。 我阅读了有关gae会话的内容,但在启动应用安全性方面需要帮助。 如何跟踪经过身份验证的用户会话?设置cookie? 初学者。 问题答案: 您可以使用cookie来做到这一点……其实并不难。您可以使用C

  • 我在spring MVC项目中实现了一个自定义身份验证提供程序。在我自己的重载authenticate()方法中,我实现了自己的身份验证,其中我构造了自己的UserPasswordAuthenticationToken()并返回对象。 现在,上述对象“UserPasswordAuthentictionToken”中的用户ID被匿名化,密码为null,权限设置为授予该用户的权限。 问题: 这是否会导