完成登陆接口
程序员文章站
2022-05-19 10:11:13
...
CookieUtils
要注意,这里我们使用了一个工具类,CookieUtils,可以在课前资料中找到,我们把它添加到learn-common
中,然后引入servlet相关依赖即可:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>
UserClient
接下来我们肯定要对用户密码进行校验,所以我们需要通过FeignClient去访问 user-service微服务:
在learn-auth中引入user-service-interface依赖:
<dependency>
<groupId>com.learn.user</groupId>
<artifactId>learn-user-interface</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
参照learn-search或者learn-goods-web,在learn-auth-service中编写FeignClient:
@FeignClient(value = "user-service")
public interface UserClient extends UserApi {
}
在learn-user-interface工程中添加api接口:
内容:
@RequestMapping("user")
public interface UserApi {
@GetMapping("query")
public User queryUser(
@RequestParam("username") String username,
@RequestParam("password") String password);
}
AuthService
在learn-auth-service:
@Service
public class AuthService {
@Autowired
private UserClient userClient;
@Autowired
private JwtProperties properties;
public String authentication(String username, String password) {
try {
// 调用微服务,执行查询
User user = this.userClient.queryUser(username, password);
// 如果查询结果为null,则直接返回null
if (user == null) {
return null;
}
// 如果有查询结果,则生成token
String token = JwtUtils.generateToken(new UserInfo(user.getId(), user.getUsername()),
properties.getPrivateKey(), properties.getExpire());
return token;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
上一篇: Shiro授权
下一篇: 给zuul网关添加过滤器