Missing converter from class java.lang.String to interface org.springframework.security.core.GrantedAuthority, this is needed for field authorities on Entry class
时间: 2024-04-12 16:28:00 浏览: 120
The error message you mentioned indicates that there is a missing converter from the `String` class to the `GrantedAuthority` interface. This converter is needed for the `authorities` field in the `Entry` class.
To resolve this issue, you can create a custom converter that converts a `String` to a `GrantedAuthority` object. Here's an example of how you can implement the converter:
```java
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
public class StringToGrantedAuthorityConverter implements Converter<String, GrantedAuthority> {
@Override
public GrantedAuthority convert(String source) {
return new SimpleGrantedAuthority(source);
}
}
```
Once you have the converter implemented, you need to register it with Spring's conversion service. You can do this by adding the following configuration to your application:
```java
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToGrantedAuthorityConverter());
}
}
```
By registering the converter, Spring will be able to convert `String` values to `GrantedAuthority` objects when needed, resolving the error you encountered.
阅读全文