Im trying to get role name in row authority but Im getting null: enter image description here Role name is a "ADMIN" or "NORMAL" string responsible for access to my web page.
With exception in console:
java.lang.NullPointerException: Cannot invoke "com.example.model.Role.getName()" because the return value of "com.example.model.UserRole.getRole()" is null
at com.example.model.User.getAuthorities(User.java:139) ~[classes/:na]
at com.example.config.JwtAuthenticationFilter.doFilterInternal(JwtAuthenticationFilter.java:67) ~[classes/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.15.jar:5.3.15]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336) ~[spring-security-web-5.6.1.jar:5.6.1]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103) ~[spring-security-web-5.6.1.jar:5.6.1]
Here is my entity:
@Entity
@Table(name = "users")
public class User implements UserDetails{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private String firstName;
private String lastName;
private String email;
private String phone;
private boolean enabled = true;
private String profile;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user")
@JsonIgnore
private Set<UserRole> userRoles = new HashSet<>();
public User() {
}
public User(String username, String password, String firstName, String lastName, String email, String phone,)
public Set<UserRole> getUserRoles() {
return userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
//getters
//setters
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Set<Authority> authorities=new HashSet<>();
this.userRoles.forEach(userRole -> {
authorities.add(new Authority(userRole.getRole().getName()));
});
return authorities;
}
@Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}
}
@Entity
public class UserRole {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userRoleId;
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private User user;
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private Role role;
public Long getUserRoleId() {
return userRoleId;
}
public void setUserRoleId(Long userRoleId) {
this.userRoleId = userRoleId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
@Entity
@Table(name="roles")
public class Role {
@Id
private Long id;
private String name;
@OneToMany(mappedBy = "role",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private Set<UserRole> useRoles = new HashSet<>();
public Role() {
}
//getters
//setters
public Set<UserRole> getUseRoles() {
return useRoles;
}
public void setUseRoles(Set<UserRole> useRoles) {
this.useRoles = useRoles;
}
}
I've tried change relation in Role and User Role class I can't find the bug in the code. I'm stuck so I will be grateful for any help.