getAuthority() 上 AuthorizationService 中的 SonarQube 警告

SonarQube warning in AuthorizationService on getAuthority()

我有一个 Spring 引导应用程序,我在调用 getAuthority() 的行中收到以下 SonarQube 警告。我该如何解决?

Controlling permissions is security-sensitive. It has led in the past to the following vulnerabilities:

@Service    
public class AuthorizationService implements UserDetailsService {

  @Autowired
  private MRepository mRepository;

  public UserDetails loadUserByUsername(String userId) {
    MEntity user = mRepository.findByName(userId);
    if (user == null) {
      throw new UsernameNotFoundException("Invalid username or password.");
    }
    return new User(user.getName(), user.getPassword(), getAuthority());
  }


  private List<SimpleGrantedAuthority> getAuthority() { //here i get the warning
    return Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN"));
  }

较新版本的 SonarQube 不仅可以为您提供代码气味、问题和错误警告,还可以提供安全热点。这些不是您的代码本身的问题。

正如后面的警告所说:

This rule flags code that controls the access to resources and actions. The goal is to guide security code reviews.

这只是意味着您应该仔细查看,因为 Sonar 已在您的代码中识别出授予权限的行。这有助于审阅者发现与安全相关的代码片段。

SonarQube 然而不能说这段代码是否安全。您需要自己确保这一点。它只是告诉你在哪里看两次。如果您觉得这段代码没问题,您可以将此 SonarQube 设置为 "Resolve as Reviewed".

另请参阅官方 SonarQube 文档:https://docs.sonarqube.org/latest/user-guide/security-hotspots/