【发布时间】:2014-08-30 07:41:22
【问题描述】:
我正在尝试使用 Jersey Jax-RS Api 和 Spring-Security 进行用户身份验证。但我能够做到这一点。 Jersey 提供了一些基本的身份验证方法,但我需要使用 spring-security。我正在尝试手动UserName 和Password 通过spring security 进行身份验证,但它抛出了空指针异常。以下是我的代码:
@Path(value = "/")
public class UserLoginApi {
@Autowired
private AuthenticationManager authenticationManager;
@POST
@Path(value = "test")
public void test() {
System.out
.println("Hello REST **************************************** ");
}
@POST
@Path(value = "login")
@Consumes(MediaType.APPLICATION_JSON)
public Response userlogin(User user, @Context HttpServletRequest request) {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
user.getUsername(), user.getPassword());
Authentication authentication = authenticationManager.authenticate(authenticationToken);
User user2 = (User) SecurityContextHolder.getContext();
System.out.println(user2);
return Response.status(200).entity("OK").build();
}
}
在此代码中,authenticationManager 为 null,因此它引发了异常。有没有另一种方法可以做到这一点?或者使用球衣Spring-Security 是不可能的?
请提供一些方法来做到这一点。
谢谢
【问题讨论】:
标签: java rest spring-security jersey