【问题标题】:Adding WebSecurityConfig causes a 404 error添加 WebSecurityConfig 导致 404 错误
【发布时间】:2023-01-12 02:28:37
【问题描述】:

当我添加我的 websecurityconfig 文件时,我收到 404 错误。我的 Websecurityconfig 如下所示。令人恼火的是这在本地有效,但在部署时却无效。我试图点击我的应用程序的头部,我希望它重定向到登录页面,但在登录之前它是 404

package org.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.userdetails.DaoAuthenticationConfigurer;
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;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.example.demo.LoginSuccessHandler;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private DataSource dataSource;

    @Bean
    public UserDetailsService userDetailsService() {
        return new CustomUserDetailsService();

    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();

    }

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(userDetailsService());
        authenticationProvider.setPasswordEncoder(passwordEncoder());

        return authenticationProvider;

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/abc").hasAnyAuthority("ROLE_one")
                .antMatchers("/def").hasAnyAuthority("ROLE_one")
                .antMatchers("/ghi").hasAnyAuthority("ROLE_one")
                .antMatchers("/jkl").hasAnyAuthority("ROLE_one")
                .antMatchers("/mno").hasAnyAuthority("ROLE_one")
                .antMatchers("/pqr").hasAnyAuthority("ROLE_one")
                .antMatchers("/stu").hasAnyAuthority("ROLE_two")
                .antMatchers("/vwx").hasAnyAuthority( "ROLE_two")
                .antMatchers("/yza").hasAnyAuthority("ROLE_two")
                .antMatchers("/bcd").hasAnyAuthority("ROLE_two")
                .antMatchers("/").permitAll()
                .and().formLogin()
                .successHandler(successHandler)
                .and().logout().permitAll();
    }


    @Autowired private LoginSuccessHandler successHandler;


}

我尝试删除它以消除 404 错误,但我需要设置安全性。

【问题讨论】:

    标签: spring spring-boot spring-mvc spring-security spring-data-jpa


    【解决方案1】:

    404 表示未找到与您尝试访问的网址对应的页面。您的 HttpSecurity 具有以下设置,这意味着任何人都可以访问您的应用程序的根目录,并且看起来没有为其定义任何页面或 index.html -

    .antMatchers("/").permitAll()
    

    您是否部署了不同的包,并且在本地为根 url 定义了一个页面,这就是您看不到 404 的原因?

    【讨论】:

    • 我有以下页面:@Controller public class HelloController { @GetMapping("/") String hello() { return "Hello World, Spring Boot!"; } 所以它应该显示我相信
    • 您应该将 @ResponseBody 与 @GetMapping 一起添加到 hello 方法中,或者将 @Controller 更改为 @RestController...
    【解决方案2】:

    原来我没有正确填写我的 application.properties 文件来连接我的数据库。修复修复了错误。

    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.hibernate.ddl-auto=create
    spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
    
    
    spring.datasource.url=jdbc:mysql://localhost:3306/<application_name>
    spring.datasource.username=<username>
    spring.datasource.password=<password>
    
    

    而且我需要在任何地方都使用 Controller 而不是 RestController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 2013-08-31
      相关资源
      最近更新 更多