Can I "autwired" in a class extending another (SimpleUrlAuthenticationSuccessHandler)?
I have a exctend class that is SimpleUrlAuthenticationSuccessHandler.
I tried to auto-scroll another one from my class annotated as @Component, but when I call this class (extended) I get this error:
java.lang.NullPointerException
com.springgestioneerrori.loginhandler.CustomAuthenticationSuccessHandler.onAuthenticationSuccess(CustomAuthenticationSuccessHandler.java:33)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.successfulAuthentication(AbstractAuthenticationProcessingFilter.java:329)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.successfulAuthentication(AbstractAuthenticationProcessingFilter.java:294)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:219)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
this is a class that extends SimpleUrlAuthenticationSuccessHandler
package com.springgestioneerrori.loginhandler;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import com.springgestioneerrori.struttura.Menu;
@Component
public class CustomAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Autowired
Menu menu;
@Override
public void onAuthenticationSuccess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Authentication authentication) throws IOException, javax.servlet.ServletException{
menu.prova(); <------ here menu is null
Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
Iterator<SimpleGrantedAuthority> i = authorities.iterator();
while(i.hasNext()){
System.out.println(i.next().toString());
}
System.out.println("sto onAuthenticationSuccess");
super.setDefaultTargetUrl("/index");
super.onAuthenticationSuccess(request, response, authentication);
}
}
Menu class
package com.springgestioneerrori.struttura;
@Component
public class Menu {
public void prova(){
System.out.println("prova");
}
}
This is the web-servlet.xml code snippet
<context:component-scan base-package="com.springgestioneerrori.struttura"/>
<context:component-scan base-package="com.springgestioneerrori.loginhandler"/>
Security configuration
....
<form-login
login-page="/login"
default-target-url="/index"
always-use-default-target="true"
authentication-success-handler-ref="customSuccessHandler"
authentication-failure-handler-ref="customFailureHandler"
username-parameter="j_username"
password-parameter="j_password" />
....
<beans:bean id="customSuccessHandler" class="com.springgestioneerrori.loginhandler.CustomAuthenticationSuccessHandler"/>
......
Menu is null when debugging
What is the problem?
+3
source to share
1 answer
You can do it. Let's assume this is your ad Menu
inside CustomAuthenticationSuccessHandler
.
private Menu menu;
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu= menu;
}
Now we can use property injection. Internal Security Configuration
<beans:bean id="menu" class="menu path goes here....">
</beans:bean>
<beans:bean id="customSuccessHandler" class="com.springgestioneerrori.loginhandler.CustomAuthenticationSuccessHandler">
<beans:property name="menu" ref="menu"/>
</beans:bean>
It should now fix your problem.
0
source to share