【发布时间】:2016-05-25 23:00:14
【问题描述】:
我有一个android应用程序,我在服务器端使用spring security进行身份验证和授权。首先,用户输入用户名和密码,并使用Rest Template通过请求将其发送到服务器控制器进行身份验证。我有完成了此用户名密码请求的身份验证部分。现在我必须为连续请求身份验证生成一个令牌。但我不知道该怎么做。这就是我的想法-
a) 生成并存储 AES 密钥。
b) 使用此 AES 密钥加密用户名 + 时间戳(您必须在此步骤后进行 base64 编码以避免任何特殊字符)。这将是您的用户令牌。
c) 使用 HMAC 密钥签署此令牌(再次将其存储在密钥库中)。
d) 将令牌与签名一起发送(使用 ; 作为分隔符)。
e) 将您在步骤 c 中获得的令牌与数据库中的用户名存储起来。
令牌将作为响应的一部分发送给客户端。 我不知道如何实现上述方法。我在网上搜索过,但没有找到任何有用的东西。 以下是我用于身份验证的代码。
try{
System.out.println("Request received... ");
Gson gson = new Gson();
TestUser userChk=new TestUser();
userChk=gson.fromJson(coaObj, TestUser.class);
Authentication authenticationToken = new UsernamePasswordAuthenticationToken(userChk.getUserName(), userChk.getPassword());
Authentication authentication = authenticationManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
System.out.println("getAuthorities is "+authentication.getAuthorities()+" getCredentials "+authentication.getCredentials()+" getDetails "+authentication.getDetails()+" getPrincipal "+authentication.getPrincipal());
return "Authenticated";
}catch(Exception er){
System.out.println("Authentication error "+er);
return "Authentication failure";
}
请有人帮我用这种方法生成令牌并管理它。
【问题讨论】:
标签: authentication spring-security token resttemplate