Spring-boot 安全性不会尊重自定义 AuthenticationProvider 的角色

Jef*_*man 1 java spring-security spring-boot spring-boot-starter

我正在创建自己的自定义身份验证提供程序。现在我只是在检查静态用户名和密码,但稍后这将被替换为更高级的东西,所以虽然我不需要在这种情况下使用自定义提供程序,但它对我没有多大帮助,因为它只是基础工作我还没有添加的附加代码。

话虽如此,这是我的代码处于损坏状态。

我的自定义身份验证提供程序:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SatAuthenticationProvider implements AuthenticationProvider {

  private static final Logger LOGGER = LoggerFactory.getLogger(SatAuthenticationProvider.class);

  public SatAuthenticationProvider() {
    LOGGER.info("*** CustomAuthenticationProvider created");
  }

  @Override
  public boolean supports(Class<? extends Object> authentication) {
    return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
  }

  @Override
  public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    LOGGER.info("*** Creating authentication");
    if (authentication.getName().equals("test") && authentication.getCredentials().equals("test")) {
      List<GrantedAuthority> grantedAuths = new ArrayList<>();
      grantedAuths.add(new SimpleGrantedAuthority("USER"));
      grantedAuths.add(new SimpleGrantedAuthority("ADMIN"));
      return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths);
    } else {
      return null;
    }

  }
}
Run Code Online (Sandbox Code Playgroud)

这是我消耗的安全配置是:

import com.comcast.iot.das.auth.SatAuthenticationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class DeviceSecurityConfig extends WebSecurityConfigurerAdapter {

  private static final Logger LOGGER = LoggerFactory.getLogger(SatAuthenticationProvider.class);

  @Autowired
  private SatAuthenticationProvider satAuthenticationProvider;

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    LOGGER.info("*** configuring http");
    http.authorizeRequests().anyRequest()
      .hasRole("USER")
      .and()
      .httpBasic();
  }

  @Autowired
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    LOGGER.info("*** Setting builder");
    auth.authenticationProvider(this.satAuthenticationProvider);
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我运行它时,如果我使用 curl 来访问没有指定用户和密码的端点,我会得到以下信息:

% curl http://localhost:8080/molecule
{
  "timestamp" : 1505925047977,
  "status" : 401,
  "error" : "Unauthorized",
  "message" : "Full authentication is required to access this resource",
  "path" : "/molecule"
}
Run Code Online (Sandbox Code Playgroud)

如果我指定了正确的用户名和密码,我会得到以下信息:

% curl -u test:test http://localhost:8080/molecule
{
  "timestamp" : 1505925033015,
  "status" : 403,
  "error" : "Forbidden",
  "message" : "Access is denied",
  "path" : "/molecule"
}
Run Code Online (Sandbox Code Playgroud)

最后,如果我指定了错误的用户名和密码,我会得到以下信息:

% curl -u test:test2 http://localhost:8080/molecule
{
  "timestamp" : 1505925199406,
  "status" : 401,
  "error" : "Unauthorized",
  "message" : "No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken",
  "path" : "/molecule"
}
Run Code Online (Sandbox Code Playgroud)

最后一点,虽然我不能让角色直接工作,但我可以让它测试用户是否经过身份验证并使用它来授予权限。这不是一个可行的解决方案,因为我需要角色,但它可能会给任何试图回答这个问题的人一些提示。

所以我可以改变 DeviceSecurityConfig 类的配置方法如下:

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    LOGGER.info("*** configuring http");
    http.authorizeRequests().anyRequest()
      .authenticated()
      .and()
      .httpBasic();
  }
Run Code Online (Sandbox Code Playgroud)

使用这个新版本的代码,我的 curl 请求似乎至少按预期工作(尽管当然无法添加角色):。这是我刚刚提到的代码编辑的 curl 结果。

没有用户名和密码:

% curl http://localhost:8080/molecule
{
  "timestamp" : 1505925444957,
  "status" : 401,
  "error" : "Unauthorized",
  "message" : "Full authentication is required to access this resource",
  "path" : "/molecule"
}
Run Code Online (Sandbox Code Playgroud)

密码错误:

% curl -u test:test2 http://localhost:8080/molecule
{
  "timestamp" : 1505925456018,
  "status" : 401,
  "error" : "Unauthorized",
  "message" : "No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken",
  "path" : "/molecule"
}
Run Code Online (Sandbox Code Playgroud)

使用以下命令的正确密码和用户名现在从我通常期望的端点返回完整响应(省略)。

% curl -u test:test http://localhost:8080/molecule
Run Code Online (Sandbox Code Playgroud)

hol*_*s83 6

角色权限应以 为前缀ROLE_

grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
Run Code Online (Sandbox Code Playgroud)