Spring Boot Security为请求返回Forbidden 404,即使我已经给出了正确的身份验证。
我正在将以下UsernamePasswordAuthenticationToken设置为安全上下文持有者
My UsernamePasswordAuthenticationToken object
。
下面是我的
-
安全配置类
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and().csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.authorizeRequests()
.antMatchers("/api/public/**").permitAll()
.antMatchers("/api/auth/seller/**").hasRole("SELLER")
.antMatchers("/api/auth/consumer/**").hasRole("CONSUMER")
.anyRequest().authenticated();
http.authenticationProvider(authenticationProvider());
http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
-
我的用户实体
package com.fresco.ecommerce.models;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
@Entity
public class User implements UserDetails {
private static final long serialVersionUID = 5536306799835655715L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Integer userId;
@Column(unique = true)
private String username;
private String password;
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
private Set<Role> roles;
public User() {
super();
}
public User(String username, String password, Set<Role> roles) {
super();
this.username = username;
this.password = password;
this.roles = roles;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "User [userId=" + userId + ", username=" + username + ", password=" + password + ", roles=" + roles
+ "]";
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.roles.stream().map(r -> new RoleGrantedAuthority(r.name())).collect(Collectors.toList());
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
我什么都试过了。当我删除hasRole时,我就可以点击api了。
我是Spring的新手,如何在禁止的地方进行调试。