简体   繁体   English

Spring Security OAuth2中的JSON请求无法运行tomcat 9

[英]JSON Request in Spring security OAuth2 not working tomcat 9

Followed this post to change the request to json. 关注帖子,将请求更改为json。

My configurations 我的配置

Filter: 过滤:

@Component
@Order(value = Integer.MIN_VALUE)
public class JsonToUrlEncodedAuthenticationFilter implements Filter {

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

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        if (Objects.equals(request.getContentType(), "application/json") && Objects.equals(((RequestFacade) request).getServletPath(), "/oauth/token")) {
            InputStream is = request.getInputStream();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            int nRead;
            byte[] data = new byte[16384];

            while ((nRead = is.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();
            byte[] json = buffer.toByteArray();

            HashMap<String, String> result = new ObjectMapper().readValue(json, HashMap.class);
            HashMap<String, String[]> r = new HashMap<>();
            for (String key : result.keySet()) {
                String[] val = new String[1];
                val[0] = result.get(key);
                r.put(key, val);
            }

            String[] val = new String[1];
            val[0] = ((RequestFacade) request).getMethod();
            r.put("_method", val);

            HttpServletRequest s = new MyServletRequestWrapper(((HttpServletRequest) request), r);
            chain.doFilter(s, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
    }
}

Request Wrapper: 请求包装器:

public class MyServletRequestWrapper extends HttpServletRequestWrapper {
    private final HashMap<String, String[]> params;

    public MyServletRequestWrapper(HttpServletRequest request, HashMap<String, String[]> params) {
        super(request);
        this.params = params;
    }

    @Override
    public String getParameter(String name) {
        if (this.params.containsKey(name)) {
            return this.params.get(name)[0];
        }
        return "";
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return this.params;
    }

    @Override
    public Enumeration<String> getParameterNames() {
        return new Enumerator<>(params.keySet());
    }

    @Override
    public String[] getParameterValues(String name) {
        return params.get(name);
    }
}

the above configuration is working in spring boot embedded tomcat. 上面的配置在Spring Boot嵌入式tomcat中工作。 Unfortunately its not working in tomcat 9.0.1 when i debug the filter , its gets values once and pass through it to doFilter(), from there its again looping, next time its get error 不幸的是,当我调试过滤器时,它在tomcat 9.0.1中不起作用,它一次获取值并将其传递给doFilter(),从那里再次循环,下次获取错误

ERROR boot.web.support.ErrorPageFilter - Forwarding to error page from request[/oauth/token] due to exception [No content to map due to end-of-input] 错误boot.web.support.ErrorPageFilter-由于发生异常而从请求[/ oauth / token]转发到错误页面[由于输入结束,无法映射任何内容]

its due to json is not there for second loop, but when springboot is used it doesn't enter it into second loop. 由于json而导致的第二循环不存在,但是当使用springboot时,它不会进入第二循环。

Any suggestion to make it work in springboot as well standalone tomcat also. 任何建议使其也可以在springboot和独立的tomcat中工作。

The issue was Duplicate Filter registration for 'springSecurityFilterChain' 问题是“ springSecurityFilterChain”的重复过滤器注册

. The Spring boot starts once in the beginning, my code also tries to register springSecurityFilterChain manually extending Spring启动从一开始就启动,我的代码也尝试手动注册springSecurityFilterChain扩展

AbstractSecurityWebApplicationInitializer.class AbstractSecurityWebApplicationInitializer.class

, which cause the trouble.After removing that, its working in spring boot as well as in standalone tomcat. 删除后,它可以在Spring Boot以及独立的tomcat中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM