我有一个类,它会激活SimpleUrlAuthenticationSuccessHandler。
我尝试自动连接我的另一个类,注释为@Component,但是当我调用这个类(扩展的类)时,我收到了这个错误:
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)
这是扩展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);
}
}
菜单类
package com.springgestioneerrori.struttura;
@Component
public class Menu {
public void prova(){
System.out.println("prova");
}
}
这是web-servlet.xml的一段代码
<context:component-scan base-package="com.springgestioneerrori.struttura"/>
<context:component-scan base-package="com.springgestioneerrori.loginhandler"/>
安全配置
....
<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"/>
...
Whene调试,菜单为空
有什么问题?
答案 0 :(得分:0)
你可以这样做。假设这是Menu
内的CustomAuthenticationSuccessHandler
声明。
private Menu menu;
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu= menu;
}
现在我们可以在这里使用属性注入。内部安全配置
<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>
现在它应该解决你的问题。