[英]How to autowire SecurityContextRepository by class implementation
I'm trying to do an Ajax login with a code i taked overthere, this is: 我正在尝试使用我在上面接过的代码进行Ajax登录,这是:
@Controller
@RequestMapping("/login")
public class AjaxLoginController {
@Autowired
@Qualifier("AuthenticationManager")
AuthenticationManager authenticationManager;
@Autowired
SecurityContextRepository repository;
@Autowired
RememberMeServices rememberMeServices;
@RequestMapping(method=RequestMethod.GET)
public void login() {}
@RequestMapping(method=RequestMethod.POST)
@ResponseBody
public String performLogin(
@RequestParam("j_username") String username,
@RequestParam("j_password") String password,
HttpServletRequest request, HttpServletResponse response)
{
UsernamePasswordAuthenticationToken token =
new UsernamePasswordAuthenticationToken(username, password);
try {
Authentication auth = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(auth);
repository.saveContext(SecurityContextHolder.getContext(), request, response);
rememberMeServices.loginSuccess(request, response, auth);
return "{\"status\": true}";
} catch (BadCredentialsException ex) {
return "{\"status\": false, \"error\": \"Bad Credentials\"}";
}
}
}
This example have an xml configuration of beans for the autowire, but my project requires to do it by class configuration. 此示例具有用于自动装配的bean的xml配置,但是我的项目要求通过类配置来完成。 I solved the first autowire issue adding this to my security class configuration:
我解决了第一个自动装配问题,将其添加到我的安全性类配置中:
@Bean
public AuthenticationManager AuthenticationManager() throws Exception {
return authenticationManager();
}
Now i'm trying to do the something with the securityContextRepository but all that i find on forums are xml configurations solutions. 现在,我正在尝试使用securityContextRepository进行操作,但是在论坛上找到的所有内容都是xml配置解决方案。 Anyone knows how configure this class as Bean to solve this?
有谁知道如何将此类配置为Bean来解决此问题? Thx.
谢谢。
HttpSessionSecurityContextRepository
is the default implementation for SecurityContextRepository
. HttpSessionSecurityContextRepository
是SecurityContextRepository
的默认实现。 Add this to the configuration: 将此添加到配置中:
@Bean
public SecurityContextRepository securityContextRepository() {
return new HttpSessionSecurityContextRepository();
}
Note that the name of the bean is securityContextRepository
and not repository
which, I think, reduces any possible ambiguity. 请注意,bean的名称是
securityContextRepository
而不是repository
,我认为这可以减少任何歧义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.