当前位置: 首页 > 知识库问答 >
问题:

禁止,http.status=403,带有jdbcAuthentication()

翟奕
2023-03-14

可能是我犯了什么愚蠢的错误。在我使用jdbcauthentication()配置身份验证后,通过Chrome登录后,会出现以下错误(在本文件的底部)。对于InMemoryAuthentication()同样有效

当我有这个的时候-

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("user").password("pass").roles("REGISTERED_USER").and().withUser("admin")
            .password("pass").roles("ADMIN");
        
}

这是我的密码-

@EnableWebSecurity
public class FplUserOperationAuthentication extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery("select m.username as username, m.password as password, "
                    + "m.enabled as enabled from stackoverflow.users_main m where m.username = ?")
            .authoritiesByUsernameQuery("select a.username as username, a.role as authority "
                    + "from stackoverflow.authorities a where a.username = ?");
    }


    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/user-profile/admin/**").hasAnyRole("ADMIN")
            .antMatchers("/user-profile/users/**").hasAnyRole("REGISTERED_USER","ADMIN")
            .antMatchers("/welcome").permitAll().and().formLogin()
            .and().csrf().disable();
    }

endpoints.shutdown.enabled=true
spring.application.name=fpl-user-operations
management.endpoints.web.exposure.include = *
spring.datasource.url = jdbc:oracle:thin:@localhost:1521/ORCLPDB
spring.datasource.username = FPLADMIN
spring.datasource.password = oracle1
spring.devtools.add-properties = false
spring.config.import=optional:configserver:http://localhost:8888
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=none
alter session set current_schema = stackoverflow;
create table users_main 
(
    username varchar2(20) primary key,
    password varchar2(20),
    enabled char(1)
);

create table authorities
(
    username varchar2(20),
    role varchar2(15),
    
    constraint authorities_c1 foreign key (username) references users_main (username)

);

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jun 21 12:18:32 IST 2021
There was an unexpected error (type=Forbidden, status=403).
2021-06-21 14:49:11.520 DEBUG 11856 --- [nio-8080-exec-5] o.s.security.web.FilterChainProxy        : Secured GET /error
2021-06-21 14:49:11.520 DEBUG 11856 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2021-06-21 14:49:11.521 DEBUG 11856 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2021-06-21 14:49:11.521 DEBUG 11856 --- [nio-8080-exec-5] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-06-21 14:49:11.537 DEBUG 11856 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2021-06-21 14:49:11.538 DEBUG 11856 --- [nio-8080-exec-5] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-06-21 14:49:11.538 DEBUG 11856 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 403
2021-06-21 14:49:11.538 DEBUG 11856 --- [nio-8080-exec-5] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request

注意-但是,如果我故意提供错误的凭据,它会说“坏凭据”。

共有1个答案

姚高爽
2023-03-14

未在configure方法中包含密码编码器

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
            .dataSource(dataSource)
            **.passwordEncoder(getPasswordEncoder())**
            .usersByUsernameQuery("select m.username as username, m.password as password, "
                    + "m.enabled as enabled from stackoverflow.users_main m where m.username = ?")
            .authoritiesByUsernameQuery("select a.username as username, a.role as authority "
                    + "from stackoverflow.authorities a where a.username = ?");
    }
 类似资料:
  • 我在Spring实现了WebSocket。一切正常,但最近我决定实施SpringSecurity。 我的MessageBroker看起来像: 我的JS客户看起来像这样: Spring Security配置: 通过我的JS客户端订阅后,我收到: 所以我决定在安全配置中添加此代码: 但在那之后我收到这种错误: 我不知道如何定义这样的bean,所以我创建了以下类: 但在编译期间,我收到这种错误: 我真的

  • 问题内容: 我在CentOS 5盒子上安装了Nginx和PHP-FPM,但是我很难使它服务于我的任何文件-无论是否为PHP。 Nginx以www-data:www-data的身份运行,默认的“欢迎使用EPEL上的nginx”站点(由root:root拥有644权限)可以很好地加载。 nginx配置文件具有 /etc/nginx/sites-enabled/*.conf 的include指令 , 而

  • 我有我的spring boot应用程序,我正在尝试添加Spring Security性,但当我通过postman发出请求时,我不断收到一个403 Forbbiden,联机时我发现我应该在我的配置中添加:“.csrf().disable()”,但它不起作用(如果我在permitAll()中放置路径为:“person/**”的方法,则所有操作都有效) 这是我的代码: 我的用户控制器: My perso

  • 问题内容: 我有一个合作伙伴,已经为我创造了一些内容供您抓取。 我可以使用浏览器访问该页面,但是当尝试使用user时,会显示。 我尝试使用,但这无济于事-可能是因为我不知道该去哪里。 1)我有什么办法可以刮取数据? 2)如果否,并且不允许合作伙伴将服务器配置为允许我访问,该怎么办? 我尝试使用的代码: 问题答案: 这不是您脚本中的问题,而是合作伙伴Web服务器安全性中的一项功能。 很难确切地说出是

  • 问题内容: 我正在尝试制作Sitecraper。我是在本地计算机上制作的,在那儿工作得很好。当我在服务器上执行相同操作时,它显示403禁止错误。我正在使用PHP简单HTML DOM解析器 。我在服务器上收到的错误是这样的: 警告:file_get_contents(http://example.com/viewProperty.html?id=7715888)[function.file- get

  • 问题内容: 我制作了供个人使用的python脚本,但不适用于Wikipedia … 这项工作: 这不起作用: 这是错误: 问题答案: 在当前代码内: Python 2.X 的Python 3.X 带有Selenium的Python 3.X(执行Javascript函数) 修改后的版本起作用的原因是因为Wikipedia检查User-Agent是“流行的浏览器”