根据角色禁用 swagger-ui
disable swagger-ui based on roles
我知道 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);
}
我建议添加一个拦截器,或者您可以在现有拦截器中处理它(如果有的话)。
在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>
拦截器class可以这样写:
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
}
没有您使用的版本或代码,很难提供帮助。不过我会尽力的。
当您使用 swagger-ui 时,您有一个公开的 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。
更多信息:
- 类似问题:
- Spring 安全配置:
https://spring.io/guides/gs/securing-web/
我可以提供更多帮助:
- 安全配置的摘录
- 您使用的 Spring-boot 的版本
我知道 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);
}
我建议添加一个拦截器,或者您可以在现有拦截器中处理它(如果有的话)。
在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>
拦截器class可以这样写:
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
}
没有您使用的版本或代码,很难提供帮助。不过我会尽力的。
当您使用 swagger-ui 时,您有一个公开的 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。
更多信息:
- 类似问题:
- Spring 安全配置: https://spring.io/guides/gs/securing-web/
我可以提供更多帮助:
- 安全配置的摘录
- 您使用的 Spring-boot 的版本