【问题标题】:Spring Boot oauth2 : Full authentication is required to access this resourceSpring Boot oauth2:访问此资源需要完全身份验证
【发布时间】:2020-11-21 04:11:43
【问题描述】:

我正在使用 Postgresql DB 在 SpringBoot 中开始配置 OAuth2。在邮递员中请求令牌后,我收到错误:

{
    "error": "unauthorized",
    "error_description": "Full authentication is required to access this resource"
}

有效载荷:

curl --location --request POST 'localhost:9000/oauth/token' \
     --header 'Content-Type: application/x-www-form-urlencoded' \
     --header 'Authorization: Basic Y2xpZW50SWQ6c2VjcmV0' \
     --data-urlencode 'grant_type=password' \
     --data-urlencode 'username=user' \
     --data-urlencode 'password=pass'

资源服务器配置:

@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter
{
    private static final String ROOT_PATTERN = "/**";


    @Override
    public void configure(HttpSecurity http) throws Exception
    {
        http.authorizeRequests()
            .antMatchers(HttpMethod.GET, ROOT_PATTERN).access("#oauth2.hasScope('read')")
            .antMatchers(HttpMethod.POST, ROOT_PATTERN).access("#oauth2.hasScope('write')")
            .antMatchers(HttpMethod.PATCH, ROOT_PATTERN).access("#oauth2.hasScope('write')")
            .antMatchers(HttpMethod.PUT, ROOT_PATTERN).access("#oauth2.hasScope('write')")
            .antMatchers(HttpMethod.DELETE, 
                                          ROOT_PATTERN).access("#oauth2.hasScope('write')");
    }
}

我检查了请求数据,一切都是正确的。我找不到问题是如何发生的。

【问题讨论】:

    标签: spring-boot spring-security spring-oauth2


    【解决方案1】:

    我发现了问题:

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
    class WebSecurityConfiguration extends WebSecurityConfigurerAdapter
    {
        private final DataSource dataSource;
        private PasswordEncoder passwordEncoder;
        private UserDetailsService userDetailsService;
    
        @Autowired
        public WebSecurityConfiguration(final DataSource dataSource)
        {
            this.dataSource = dataSource;
        }
    
        @Override
        protected void configure(final AuthenticationManagerBuilder auth) throws Exception
        {
            auth.userDetailsService(userDetailsService())
                    .passwordEncoder(passwordEncoder());
        }
    
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception
        {
            return super.authenticationManagerBean();
        }
    
        @Bean
        public PasswordEncoder passwordEncoder()
        {
            if (passwordEncoder == null) {
                this.passwordEncoder = new BCryptPasswordEncoder();
            }
    
            return passwordEncoder;
        }
    
        @Bean
        public UserDetailsService userDetailsService()
        {
            if (userDetailsService == null) {
                userDetailsService = new JdbcDaoImpl();
                ((JdbcDaoImpl) userDetailsService).setDataSource(dataSource);
            }
    
            return userDetailsService;
        }
    }
    

    如您所见,我将JdbcDaoImpl 用于UserDetailsService,默认情况下它查询public 架构,但我的架构是别的东西。

    现在更改配置如下:

    spring.datasource.url=jdbc:postgresql://localhost:5432/MY_DB?currentSchema=MY_SCHEMA_NAME
    
    

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 2020-10-25
      • 2016-10-03
      • 2015-01-08
      • 2019-01-01
      • 1970-01-01
      • 2016-11-11
      • 2020-03-30
      • 2022-01-03
      相关资源
      最近更新 更多