springboot实现登录接口token
时间: 2023-09-03 16:16:51 浏览: 57
好的,我可以帮你解答这个问题。在Spring Boot中实现登录接口需要实现以下步骤:
1. 创建一个登录接口的Controller类,并添加一个POST请求的处理方法。
```java
@RestController
@RequestMapping("/api/login")
public class LoginController {
@Autowired
private JwtUtil jwtUtil;
@PostMapping
public ResponseEntity<?> login(@RequestBody User user) {
// 处理登录逻辑
// ...
// 生成token
String token = jwtUtil.generateToken(user.getUsername());
// 返回token
return ResponseEntity.ok(new JwtResponse(token));
}
}
```
2. 在登录接口的处理方法中,处理登录逻辑并生成token。这里我们使用JwtUtil来生成token,JwtUtil需要引入相关依赖和配置,具体可以参考网上的教程。
3. 创建一个JwtResponse类,用于返回token给前端。
```java
public class JwtResponse {
private final String token;
public JwtResponse(String token) {
this.token = token;
}
public String getToken() {
return token;
}
}
```
4. 在Spring Security的配置类中,配置token的认证方式。
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtUtil jwtUtil;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/api/login").permitAll()
.anyRequest().authenticated()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(new JwtFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class);
}
// ...
}
```
5. 在Spring Security的配置类中,添加一个JwtFilter用于验证token。
```java
public class JwtFilter extends OncePerRequestFilter {
@Autowired
private JwtUtil jwtUtil;
public JwtFilter(JwtUtil jwtUtil) {
this.jwtUtil = jwtUtil;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String authorizationHeader = request.getHeader("Authorization");
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
String token = authorizationHeader.substring(7);
String username = jwtUtil.extractUsername(token);
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
if (jwtUtil.validateToken(token, userDetails)) {
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
userDetails, null, userDetails.getAuthorities()
);
usernamePasswordAuthenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
}
}
}
filterChain.doFilter(request, response);
}
}
```
6. 最后,在前端登录页面中,发送POST请求到登录接口,将用户名和密码作为请求体,获取token并存储在本地。
以上就是一个简单的Spring Boot实现登录接口token的示例,希望能对你有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)