mirror of https://github.com/longtai-cn/hippo4j
Fix call /hippo4j/v1/cs/auth/users/info/xxx error when set hippo4j.core.auth.enabled = false (#781)
parent
ac7e16b891
commit
65ae17241d
@ -0,0 +1,21 @@
|
||||
package cn.hippo4j.auth.config;
|
||||
|
||||
import cn.hippo4j.auth.filter.RewriteUserInfoApiFilter;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
public class FilterConfig {
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<RewriteUserInfoApiFilter> userInfoApiFilterRegistrationBean() {
|
||||
FilterRegistrationBean<RewriteUserInfoApiFilter> registration = new FilterRegistrationBean<>();
|
||||
registration.setFilter(new RewriteUserInfoApiFilter());
|
||||
registration.addUrlPatterns("/*");
|
||||
return registration;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package cn.hippo4j.auth.filter;
|
||||
|
||||
import cn.hippo4j.auth.toolkit.AuthUtil;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* When anonymous login is enabled, an error will be reported when viewing the current user information.
|
||||
* Modify the URI to query the default administrator information.
|
||||
*
|
||||
* before:hippo4j/v1/cs/auth/users/info or hippo4j/v1/cs/auth/users/info/xxx
|
||||
* after:hippo4j/v1/cs/auth/users/info/admin
|
||||
*/
|
||||
public class RewriteUserInfoApiFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
boolean enableAuthentication = AuthUtil.enableAuthentication;
|
||||
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
|
||||
String path = httpRequest.getRequestURI();
|
||||
if (!enableAuthentication && path.contains("users/info")) {
|
||||
httpRequest.getRequestDispatcher("/hippo4j/v1/cs/auth/users/info/admin").forward(servletRequest, servletResponse);
|
||||
return;
|
||||
}
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package cn.hippo4j.auth.toolkit;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AuthUtil {
|
||||
|
||||
public static boolean enableAuthentication;
|
||||
|
||||
@Value("${hippo4j.core.auth.enabled:true}")
|
||||
public void setEnableAuthentication(boolean enabled) {
|
||||
AuthUtil.enableAuthentication = enabled;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue