【问题标题】:Basic Authentication of Rest API'sRest API 的基本身份验证
【发布时间】:2019-07-31 12:12:47
【问题描述】:

我有一些使用 springboot 创建的 api。现在我想保留其余 api 的身份验证。只有特定用户才能访问他的数据。 我在下面列出的 postgress 数据库中有两个表。

1.在没有标题的情况下点击 api,使用正确的响应代码可以正常工作

2.使用基本身份验证访问 api,其中用户名和密码应与 db 匹配

我的期望是状态码为 200 的正确 json 响应,但仍然是 401 Unauthorized。为什么?

我的 SecurityConfig.java 文件

package com.demo.itemservice.config;

import javax.sql.DataSource;

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.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration 
@EnableWebSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter
{

     @Autowired
     private DataSource dataSource;



    @Override
    protected void configure(HttpSecurity http) throws Exception 
    {
        http
         .csrf().disable()
         .authorizeRequests().anyRequest().authenticated()
         .and()
         .httpBasic();


    }


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.jdbcAuthentication().dataSource(dataSource)
          .usersByUsernameQuery("select userid,pgp_sym_decrypt(password,'mySecretKey') from users where userid=?")
          .authoritiesByUsernameQuery("select userid, role from userroles where userid=?");
    }

}

【问题讨论】:

  • 你的密码栏是 text 还是 bytea?
  • 密码是 bytea 类型

标签: java spring


【解决方案1】:

你可以试试下面的代码吗?我将 anyRequest() 替换为 antMatchers()

 http
         .csrf().disable()
         .authorizeRequests().antMatchers("/barista/**").authenticated()
         .and()
         .httpBasic();

【讨论】:

【解决方案2】:

不允许使用 pgp_sym_decrypt 解密 bytea 数据。这是为了避免输出无效的字符数据。使用 pgp_sym_decrypt_bytea 解密原始文本数据是可以的。 参考Postgres Documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2016-06-08
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2016-03-20
    相关资源
    最近更新 更多