【发布时间】:2014-03-21 00:23:25
【问题描述】:
我正在使用 spring4 和 spring security、spring data jpa、spring boot。成功用户身份验证后,我需要进行一些处理(例如,将一些数据保存到会话中)。所以我的代码:
@Component
public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Autowired UserRepository userRepository;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
request.getSession().setAttribute("attribute1",userService.findBySomething() );
super.onAuthenticationSuccess(request, response, authentication);
}
}
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().successHandler(new MyAuthenticationSuccessHandler())
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
//another methods ..
}
调用 userService.findBySomething() 后: java.lang.NullPointerException:在 org.pckg.MyAuthenticationSuccessHandler.onAuthenticationSuccess 处为空(MyAuthenticationSuccessHandler.java:40) 当我在其他地方(例如控制器)调用此 userService.findBySomething() 时,调用成功。
【问题讨论】:
标签: spring hibernate spring-mvc spring-security