我将很高兴知道我如何存储一个用户在http会话后,他登录使用Spring Boot 。
基本上,我想在我的会话中存储实体“Teacher”,以控制用户在登录或未登录时可以访问的页面。
这是我想存储在http会话中的实体:
@Entity
public class Teacher implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 254)
private String name;
@Column(nullable = false, length = 254, unique = true)
private String email;
@Column(nullable = false, length = 254, unique = true)
private String password;
@Column(nullable = false, length = 20)
@Enumerated(EnumType.STRING)
private Role role;
@JsonIgnore
@OneToMany(mappedBy = "teacher")
private List<Scheduling> schedulings = new ArrayList<>();
这是我的登录验证器:
@PostMapping(value = "/validateLogin")
public ResponseEntity<Object> validateLogin(@RequestBody Map<String, String> user) {
try {
Teacher t = teacherRepository.findUser(user.get("name"), user.get("email"));
if (t != null && encoder.matches(user.get("password"), t.getPassword())) {
//
//I WANT TO STORE THE ENTITY HERE BUT I DON'T KNOW HOW
//
return ResponseEntity.status(HttpStatus.OK)
.body(Map.of("result", "ok", "details", "login approved"));
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(Map.of("result", "error", "details", "login denied"));
}
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("result", "error", "details", e.getMessage()));
}
}
我想验证用户是否在此处登录:
@GetMapping("/mainPage")
public String mainPage() {
//
//IF USER IN HTTP SESSION{
//
return "mainPage";
//ELSE RETURN YOU ARE NOT LOGGED IN!
}
1条答案
按热度按时间hjqgdpho1#
您可以像这样修改
validateLogin
和类似
mainPage