title: A granted authority textual representation is required in Spring Security
body: <p>I have a problem with registration permissions in Spring Security <br>
  I can't do the methods register </p>
  
  <p>I tried to set access to each path but it didn't help</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@RestController
  public class UserController {
  
      private UserService userService;
  
      public UserController(UserService userService) {
          this.userService = userService;
      }
  
      @PostMapping("/register")
          public Long register(@RequestBody User user){
          return userService.register(user);
      }
  }
  </code></pre>
  
  <p><strong>SecurityConfig</strong> </p>
  
  <pre><code>@Configuration
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Bean
      public PasswordEncoder getPasswordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      private UserDetailsServiceImpl userDetailsService;
  
      public SecurityConfig(UserDetailsServiceImpl userDetailsService) {
          this.userDetailsService = userDetailsService;
      }
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService);
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().disable();
          http.headers().disable();
          http.authorizeRequests().
                  antMatchers("/").permitAll()
                  .antMatchers("/register").permitAll();
      }
  }
  </code></pre>
  
  <p><strong>UserSerice</strong></p>
  
  <pre><code>@Service
  public class UserService {
  
      private UserRepository userRepository;
      private PasswordEncoder passwordEncoder;
  
      public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
          this.userRepository = userRepository;
          this.passwordEncoder = passwordEncoder;
      }
  
      public Long register(User user){
          user.setPassword(passwordEncoder.encode(user.getPassword()));
          userRepository.save(user);
          return user.getId();
      }
  }
  </code></pre>
  
  <p><strong>User Model</strong></p>
  
  <pre><code>import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.UserDetails;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import java.util.*;
  
  @Entity
  public class User implements UserDetails {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private String userName;
      private String lastName;
      private String password;
      private String role;       
  
      public User() {
      }
  
     ..get and set...
  
      @Override
      public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
          List&lt;GrantedAuthority&gt; listRole = new ArrayList&lt;GrantedAuthority&gt;();
  
          listRole.add(new SimpleGrantedAuthority(role));
          return listRole;
      }
  
      @Override
      public String getPassword() {
          return password;
      }
  
      @Override
      public String getUsername() {
          return userName;
      }
  
      @Override
      public boolean isAccountNonExpired() {
          return true;
      }
  
      @Override
      public boolean isAccountNonLocked() {
          return true;
      }
  
      @Override
      public boolean isCredentialsNonExpired() {
          return true;
      }
  
      @Override
      public boolean isEnabled() {
          return true;
      }
  }
  </code></pre>
  
  <p>Error</p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: A granted authority textual
    representation is required    at
    org.springframework.util.Assert.hasText(Assert.java:284)
    ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE]    at
    org.springframework.security.core.authority.SimpleGrantedAuthority.(SimpleGrantedAuthority.java:38)
    ~[spring-security-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]   at
    com.xxx.xx.models.User.getAuthorities(User.java:71) ~[classes/:na]    at
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
    Method) ~[na:na]  at
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    ~[na:na]  at
    java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    ~[na:na]  at
    java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]</p>
  </blockquote>