gpt4 book ai didi

java - 在 spring security 3.0 中添加自定义过滤器

转载 作者:行者123 更新时间:2023-11-30 09:30:27 26 4
gpt4 key购买 nike

我正在尝试添加另一个名为 captcha verification filter 的过滤器以及 spring security 的身份验证过滤器。我收到此错误。我缺少什么?bean 初始化失败;嵌套异常是 org.springframework.beans.ConversionNotSupportedException:无法将类型“java.util.LinkedHashMap”的属性值转换为属性“filterChainMap”所需的类型“java.util.Map”;嵌套异常是 java.lang.IllegalStateException:无法将类型 [com.asu.edu.base.vo.CaptchaFilterVO] 的值转换为属性“filterChainMap[/**][3]”所需的类型 [javax.servlet.Filter] : 未找到匹配的编辑器或转换策略

我的 .java 文件

public class CaptchaFilterVO {
@Autowired
private ReCaptcha reCaptcha = null;

public void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
FilterChain chain) throws IOException, ServletException {
String recaptcha_response = req.getParameter("recaptcha_response_field");
String recaptcha_challenge = req.getParameter("recaptcha_challenge_field");
String remoteAddress = req.getRemoteAddr();
ReCaptchaResponse reCaptchaResponse = this.reCaptcha.checkAnswer(
remoteAddress, recaptcha_challenge, recaptcha_response);
if (!reCaptchaResponse.isValid()) {

System.out.println("Captcha worong. Please try again.");

}
else
{
System.out.println("Captcha correct. No need to try again.");
}
chain.doFilter(req, res);
}
}

springsecurity.xml

     <http auto-config="true">
<!-- intercept-url pattern="/welcome*" access="ROLE_DEPARTMENT_MGR,ROLE_REGULAR_EMP,ROLE_GUEST_USR,ROLE_CORPORATE_MGR" />
<intercept-url pattern="/admin*" access="ROLE_ADMIN" /-->
<intercept-url pattern="/login" filters="none" />
<intercept-url pattern="/resources*" filters="none" />
<intercept-url pattern="/register" filters="none" />
<intercept-url pattern="/logout" filters="none" />
<intercept-url pattern="/loginfailed" filters="none" />
<intercept-url pattern="/admin*" access="ROLE_ADMIN" />
<intercept-url pattern="/Dashboard*" access="ROLE_DEPARTMENT_MGR,ROLE_REGULAR_EMP,ROLE_CORPORATE_MGR" />
<intercept-url pattern="/*" access="IS_AUTHENTICATED_FULLY"/>
<form-login login-page="/login" default-target-url="/"
authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
<custom-filter ref="captchaVerifierFilter" after="FORM_LOGIN_FILTER"/>

</http>
<beans:bean id="captchaVerifierFilter" class="com.asu.edu.base.vo.CaptchaFilterVO"/>
<beans:bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>
<beans:bean id="myfilterChainProxy" class="org.springframework.security.web.FilterChainProxy">
<filter-chain-map path-type="ant">
<filter-chain pattern="/*" filters="springSecurityFilterChain,captchaVerifierFilter"/>
</filter-chain-map>
</beans:bean>

编辑:公共(public)类 CaptchaFilterVO 扩展 OncePerRequestFilter 实现 javax.servlet.Filter现在我已经扩展了 OncePerRequestFilter 类,但现在我面临崩溃。请任何帮助。org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [org.springframework.http.HttpRequest]:指定的类是一个接口(interface) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)

最佳答案

您的方法名称 (doFilterInternal) 表明您想要扩展 org.springframework.web.filter.OncePerRequestFilter

添加:

extends OncePerRequestFilter

在类(class)名称之后。

OncePerRequestFilter 实现了 javax.servlet.Filter

和...

Filter base class that guarantees to be just executed once per request,
on any servlet container. It provides a doFilterInternal
method with HttpServletRequest and HttpServletResponse arguments.

关于java - 在 spring security 3.0 中添加自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13266413/

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