【发布时间】: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 类型