API客户端有用户名和密码。
在postman中,客户端选择基本身份验证并输入用户名和密码。当向服务器发出请求时,postman将基本身份验证令牌发送到服务器。
现在,在服务器端,我们只有一个包含用户名和密码的表。我们没有为客户端维护任何角色,这就是为什么我们只有一个表。现在,如何使用基本身份验证令牌来解析用户名和密码。并解决了如何将其与数据库中存储的用户名和密码进行比较的问题。
我是SPRING的初学者,很长一段时间都在努力实现它。请帮帮忙。
发布于 2017-05-05 22:36:22
您可以阅读此主题http://www.svlada.com/jwt-token-authentication-with-spring-boot/。这是非常有用的。它基于jwt身份验证。
发布于 2017-05-05 20:55:46
尝试使用Spring安全性。这是简单和健壮的。您可以通过创建WebSecurityConfigAdapter来为您的应用程序启用http basic安全性。https://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#jc-httpsecurity
如果你不想使用Spring Security,你可以编写你的自定义实现。Http基本身份验证令牌只不过是用户名和密码的冒号分隔的base64编码形式。例如,用户名和密码以用户名:密码的base64编码形式发送。所以你可以添加一个SecurityFilter,如下所示。
public class SecurityFilter implements Filter{
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
String auth = httpRequest.getHeader("Authorization");
if (auth != null && auth.startsWith("Basic")) {
String base64Auth = auth.substring("Basic".length()).trim();
String credentials = new String(Base64.getDecoder().decode(base64Auth),
Charset.forName("UTF-8"));
String[] usernamePassword = credentials.split(":",2);
// Query to database and do authentication here using username and password.
//if(successfull)
// chain.doFilter(req, resp);
//else
// return 401 error
}
}https://stackoverflow.com/questions/43804751
复制相似问题