I am implementing a custom filter by extending UsernamePasswordAuthenticationFilter and overriding the requiresAuthentication method. However, in that method, I need to have access to the filter chain. Therefore I autowired it in. However, when trying to build, it fails saying that there is no such bean for the FilterChain.
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/security-context.xml
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
security-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<security:http
realm="Protected API"
use-expressions="true"
auto-config="false"
create-session="stateless"
entry-point-ref="unauthorizedEntryPoint">
<security:intercept-url pattern="/User/sign_up/*" access="permitAll" />
<security:intercept-url pattern="/User/authenticate/**" access="permitAll" />
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<security:custom-filter ref="authenticationTokenFilter" position="FORM_LOGIN_FILTER" />
</security:http>
<authentication-manager alias="authenticationManager">
<authentication-provider ref="customAuthenticationProvider" />
</authentication-manager>
<beans:bean id="customAuthenticationProvider" class="pathTo:CustomAuthenticationProvider"></beans:bean>
<beans:bean id="unauthorizedEntryPoint" class="pathTo:UnauthorizedEntryPoint" />
<beans:bean id="mySuccessHandler" class="pathTo:SuccessHandler" />
<beans:bean
class="pathTo:AuthenticationTokenFilter"
id="authenticationTokenFilter">
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="postOnly" value="false" />
<beans:property name="authenticationSuccessHandler" ref="mySuccessHandler" />
</beans:bean>
<context:component-scan base-package="base package" />
<context:annotation-config />
</beans:beans>
Filter:
public class AuthenticationTokenFilter extends UsernamePasswordAuthenticationFilter {
@Autowired
FilterChain chain;
@Override
protected boolean requiresAuthentication(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("here");
boolean retVal = false;
String username = request.getHeader("j_username");
String password = request.getHeader("j_password");
System.out.println("username: " + username + " password: " + password);
if (username != null && password != null) {
Authentication authResult = null;
try {
authResult = attemptAuthentication(request, response);
System.out.println(authResult.getName());
if (authResult == null) {
retVal = false;
}
} catch (AuthenticationException failed) {
try {
System.out.println("auth failed");
unsuccessfulAuthentication(request, response, failed);
} catch (IOException e) {
retVal = false;
} catch (ServletException e) {
retVal = false;
}
retVal = false;
}
try {
successfulAuthentication(request, response, chain, authResult);
} catch (IOException e) {
retVal = false;
} catch (ServletException e) {
retVal = false;
}
return false;
} else {
System.out.println("not calling authenticate");
retVal = true;
}
return retVal;
}
protected String obtainPassword(HttpServletRequest request) {
return request.getHeader("j_password");
}
protected String obtainUsername(HttpServletRequest request) {
return request.getHeader("j_username");
}
Is there another way to do this without the filterChain? Otherwise, it should be able to do this since filterChain is defined in web.xml.
Any Thoughts on how to solve this?