我正在開發 Spring MVC 專案。我使用 Spring Security 來保護我的專案的 url。雖然我點擊登錄按鈕登錄成功,但type=Forbidden, status=403在localhost:8092/user/indexurl 上出現錯誤。我認為 Spring Security 會中斷 url。
下面是代碼Spring Security:
自定義用戶詳情
public class CustomUserDetail implements UserDetails {
private User user;
public CustomUserDetail(User user) {
super();
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(user.getRole());
return List.of(authority);
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getEmail();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
UserDetailsS??erviceImpl
public class UserDetailsServiceImpl implements UserDetailsService{
@Autowired
private UserRepo userRepo;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepo.getUserByUserName(username);
if(user == null)
{
throw new UsernameNotFoundException(username);
}
CustomUserDetail customUserDetail = new CustomUserDetail(user);
return customUserDetail;
}
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.antMatchers("/**").permitAll()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/user/index");
}
下面是Controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserRepo userRepo;
@RequestMapping(value = "/index")
public String login(Model mdl, Principal principal)
{
User user = userRepo.getUserByUserName(principal.getName());
mdl.addAttribute("user", user);
return "user/user-dashboard";
}
}
下面是我的user-dashboard.htmlwhile 位于src/main/resources/templates/user.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1> Details of User</h1>
<p th:text="${user.name}"></p>
<p th:text="${user.email}"></p>
</body>
</html>
我在點擊登錄按鈕時得到了那個時間 urlhttp://localhost:8092/user/index
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Mar 19 18:16:13 IST 2022
There was an unexpected error (type=Forbidden, status=403).
Forbidden
資料庫快照:

uj5u.com熱心網友回復:
我認為你應該檢查兩件事。
- 用戶在資料庫中的角色資料是什么?
- 我認為它應該在資料庫中有一個前綴“ROLE_”,如 ROLE_ADMIN。
- 檢查應加密并保存在資料庫中的密碼。
- Spring security 的 DaoAuthenticationProvider 使用默認
PasswordEncoder的PasswordEncoderFactories.createDelegatingPasswordEncoder()方法。 - 所以你必須使用
PasswordEncoderFactories.createDelegatingPasswordEncoder()實體的encode方法保存用戶資料的加密密碼。passwordEncoder.encode(password).
uj5u.com熱心網友回復:
在 db 中保存時,將其保存為 role_user 全部大寫。spring security 將角色比較為 (role hasRole value)==db value。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/445627.html
