我知道swagger-ui可以在spring-boot应用程序上使用@Profile完全禁用,但我仍然希望某些特权用户能够访问swagger-ui,而不是完全禁用。
有没有办法做到这一点。
更新:
目前,我正在使用interceptor方法,但我不想使用这种方法。
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if(request.getRequestURI().contains("swagger") &&
!request.isUserInRole("XX_YY_ZZ")) {
response.sendError(403, "You are not authorized to access "); }
return super.preHandle(request, response, handler);
}发布于 2019-12-27 17:05:38
如果没有您使用的版本或代码,就很难提供帮助。但我会尽我所能。
当您使用swagger时,您有一个公开的URL来访问您的文档(通常是/swagger-ui.html)。您使用的是spring-boot,讨论的是用户限制,所以我假设您可以使用spring-boot-starter-security。使用spring-boot-starter-security,您可以轻松地配置您想要保护的URL (例如,关于用户角色)。
下面是一个可以工作的示例配置:
@Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// the rest of your configuration
http.authorizeRequests().mvcMatchers("/swagger-ui.html").hasRole("DEVELOPER")
}您可以像使用控制器公开的任何URL一样保护swagger URL。
有关详细信息,请参阅:
类似的问题:How to configure Spring Security to allow Swagger URL to be accessed without authentication
我可以帮更多的忙:
使用安全configuration
发布于 2019-12-27 16:35:16
我建议添加一个拦截器,或者你可以在你的退出拦截器中处理它,如果你有。
在spring配置文件中:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/swager-url" />
<ref bean="swagerInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<bean id="swagerInterceptor" class="com.security.SwagerInterceptor">
<property name="userService" ref="userService" />
</bean>可以编写类似于以下内容的拦截器类:
public class SwagerInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//Take out user information from the request and validate
}https://stackoverflow.com/questions/59503245
复制相似问题