如果我的問題看起來像是一個新手會問的問題,道歉。我對春天的世界很陌生。我正在使用Spring Security來驗證用戶身份。認證工作正常,但認證成功後,我想春天調用控制器方法.`如何在Spring Security中處理登錄成功
@RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public JwtAuthenticationResponse userLogin() {
System.out.println("Login Success");
JwtUser user = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String token = userService.generateToken(authenticationService.loadUserByUsername(user.getUsername()));
JwtAuthenticationResponse response = new JwtAuthenticationResponse(token, user.getAuthorities().toArray(),user.getUsername());
return response;
}
}
在春季安全配置添加以下
http.authorizeRequests().anyRequest().permitAll().and().formLogin().loginProcessingUrl("/login").defaultSuccessUrl("/login");
http.addFilterBefore(filter,UsernamePasswordAuthenticationFilter.class);
在這種情況下,它返回一個默認Spring登錄表單。它不會調用我的控制器方法。
我做了使用
http://localhost:8080/myapp/login
有人建議我有什麼做的調用控制器後,登錄成功後,才能登錄後發送身份驗證令牌的請求。
感謝任何幫助!
謝謝
@Ankit我沒有使用任何安全配置xml文件。我擴展了WebSecurityConfigurerAdapter並在AbstractAnnotationConfigDispatcherServletInitializer中進行了配置。我正在使用Spring MVC。 – dharan