gpt4 book ai didi

java - Spring 启动 + Spring 安全 : how to suppress the basic auth form

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:25 24 4
gpt4 key购买 nike

美好的一天。

我在 Spring 引导自动配置应用程序的上下文中使用 Spring 安全性。我的目基本 xxxxx”。

为此,我声明了一个过滤器:

@Bean
@Order(Integer.MAX_VALUE)
public Filter customAuthFilter() {
return new Filter() {

@Override
public void init(FilterConfig fc) throws ServletException {
}

@Override
public void doFilter(ServletRequest sreq, ServletResponse sresp, FilterChain fc) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) sreq;
HttpServletResponse resp = (HttpServletResponse) sresp;

fc.doFilter(req, resp);
log.info("filter");
log.info("status " + resp.getStatus());
if(resp.getStatus() == 401) {
resp.setHeader("WWW-Authenticate", "Client-driven");
}
}

@Override
public void destroy() {
}
};

从日志中我看到我的过滤器被应用程序成功识别并参与处理响应(我看到来自 doFilter 的日志消息)。但是浏览器收到的实际响应仍然包含标准的“WWW-Authenticate” header 。似乎有人覆盖了我的 header ,因为我不知道它到底是谁。

有人可以给个建议吗?

最佳答案

使用自定义 EntryPoint 解决了问题:

protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/rest/**").authenticated()
.and().httpBasic().authenticationEntryPoint(new AuthenticationEntryPoint() {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
String requestedBy = request.getHeader("X-Requested-By");
log.info("X-Requested-By: " + requestedBy);
if(requestedBy == null || requestedBy.isEmpty()) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.addHeader("WWW-Authenticate", "Basic realm=Cascade Realm");
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
} else {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.addHeader("WWW-Authenticate", "Application driven");
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}
}
});
}

关于java - Spring 启动 + Spring 安全 : how to suppress the basic auth form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26202567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com