如何在Spring Security中处理登录成功
如果我的问题看起来像是新手会问的,我道歉。我是春天世界的新手。我正在使用Spring Security对用户进行身份验证。身份验证工作正常,但在身份验证成功后,我希望Spring调用控制器方法`如何在Spring Security中处理登录成功,spring,spring-mvc,authentication,spring-security,Spring,Spring Mvc,Authentication,Spring Security,如果我的问题看起来像是新手会问的,我道歉。我是春天世界的新手。我正在使用Spring Security对用户进行身份验证。身份验证工作正常,但在身份验证成功后,我希望Spring调用控制器方法` @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public JwtAuthenticationRespons
@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;
}
}
在Spring安全配置中,我添加了以下内容
http.authorizeRequests().anyRequest().permitAll().and().formLogin().loginProcessingUrl("/login").defaultSuccessUrl("/login");
http.addFilterBefore(filter,UsernamePasswordAuthenticationFilter.class);
在本例中,它返回一个默认的Spring登录表单。它没有调用我的控制器方法
我是用
http://localhost:8080/myapp/login
是否有人建议我在登录成功后调用控制器,以便在登录后发送身份验证令牌
谢谢你的帮助
谢谢如果您提供了用于登录的自定义控制器,请删除以下内容
.formLogin().loginProcessingUrl("/login").defaultSuccessUrl("/login")
从httpSecurity
配置。这将删除SpringSecurity的默认登录页面
或者你可以这样做:我发现了我犯的错误。我不需要为登录声明请求映射。我意识到春天会照顾它 @Ankit我没有使用任何安全配置xml文件。我扩展了WebSecurityConfigureAdapter,并在AbstractAnnotationConfigDispatchers ServletInitializer中进行了配置。我正在使用SpringMVC。我没有使用controller进行用户验证。我正在尝试使用Spring安全性进行用户验证,如果验证成功,我希望调用控制器将响应发送回。