Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在我的项目中应用了Spring安全性之后,控制器不工作,每当我调用rest控制器时,它只会用404代码响应_Java_Spring Boot_Spring Security_Jwt_Http Status Code 404 - Fatal编程技术网

Java 在我的项目中应用了Spring安全性之后,控制器不工作,每当我调用rest控制器时,它只会用404代码响应

Java 在我的项目中应用了Spring安全性之后,控制器不工作,每当我调用rest控制器时,它只会用404代码响应,java,spring-boot,spring-security,jwt,http-status-code-404,Java,Spring Boot,Spring Security,Jwt,Http Status Code 404,在我的项目中应用了Spring安全性之后,控制器不工作,每当我调用rest控制器时,它只是用404代码响应 这是我的Spring安全配置类 @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder passwordEncoder() { return

在我的项目中应用了Spring安全性之后,控制器不工作,每当我调用rest控制器时,它只是用404代码响应

这是我的Spring安全配置类

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().permitAll();
    }
}
我的Application.properties文件

spring.datasource.url = jdbc:mysql://192.168.1.62:3306/dummy_users?useSSL=false
spring.datasource.username = root
spring.datasource.password = MySQL62$$
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.hibernate.ddl-auto = update
我的初始数据加载器类

package com.dummy.users.auth.config;

import com.dummy.users.auth.entity.Privilege;
import com.dummy.users.auth.entity.UsersProfileEntity;
import com.dummy.users.auth.entity.UsersRoles;
import com.dummy.users.auth.repository.PrivilegeRepository;
import com.dummy.users.auth.repository.UserProfileRepository;
import com.dummy.users.auth.repository.UsersRolesRepository;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class InitialDataLoader implements ApplicationListener<ContextRefreshedEvent> {

    boolean alreadySetup = false;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private UsersRolesRepository usersRolesRepository;

    @Autowired
    private PrivilegeRepository privilegeRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    @Transactional
    public void onApplicationEvent(ContextRefreshedEvent event) {

        if (alreadySetup) {
            return;
        }
        Privilege readPrivilege
                = createPrivilegeIfNotFound("READ_PRIVILEGE");
        Privilege writePrivilege
                = createPrivilegeIfNotFound("WRITE_PRIVILEGE");

        List<Privilege> adminPrivileges = Arrays.asList(
                readPrivilege, writePrivilege);
        createRoleIfNotFound("ROLE_ADMIN", adminPrivileges);
        createRoleIfNotFound("ROLE_USER", Arrays.asList(readPrivilege));

//        UsersRoles adminRole = usersRolesRepository.findByName("ROLE_ADMIN");
//        UsersProfileEntity user = new UsersProfileEntity();
//        user.setFirstName("KarthickRaj");
//        user.setLastName("Rathinakumar");
//        user.setPhoneNumber("95245356782");
//        user.setPassword(passwordEncoder.encode("2342423$$#"));
//        user.setEmailId("rkarthickraj@gmail.com");
//        user.setStatus("Active");
//        user.setRoles(Arrays.asList(adminRole));
//        user.setEnabled(true);
//        userProfileRepository.save(user);
        alreadySetup = true;
    }

    @Transactional
    private Privilege createPrivilegeIfNotFound(String name) {

        Privilege privilege = privilegeRepository.findByName(name);
        if (privilege == null) {
            privilege = new Privilege();
            privilege.setName(name);
            privilegeRepository.save(privilege);
        }
        return privilege;
    }

    @Transactional
    private UsersRoles createRoleIfNotFound(
            String name, Collection<Privilege> privileges) {

        UsersRoles role = usersRolesRepository.findByName(name);
        if (role == null) {
            role = new UsersRoles();
            role.setName(name);
            role.setPrivileges(privileges);
            usersRolesRepository.save(role);
        }
        return role;
    }

}
MyController类,当我访问控制器时it响应404问题

 @RestController
    public class UsersLinkAdminController {

        @GetMapping("/")
        public String getUsersStatus() {
            return "<!DOCTYPE html>\n"
                    + "<html>\n"
                    + "<head>\n"
                    + "<title>Page Title</title>\n"
                    + "<style>\n"
                    + "body {\n"
                    + "  background-color: black;\n"
                    + "  text-align: center;\n"
                    + "  color: white;\n"
                    + "  font-family: Arial, Helvetica, sans-serif;\n"
                    + "}\n"
                    + "</style>\n"
                    + "</head>\n"
                    + "<body>\n"
                    + "\n"
                    + "<h1>Welcome to Dummy Oraganization</h1>\n"
                    + "<p>Users Module Running Sucessfully</p>\n"
                    + "<p>For More Details Call Support team</p>\n"
                    + "<img src=\"avatar.png\" alt=\"Avatar\" style=\"width:200px\">\n"
                    + "\n"
                    + "</body>\n"
                    + "</html>";
        }

    }
@RestController
公共类UsersLinkAdminController{
@GetMapping(“/”)
公共字符串getUsersStatus(){
返回“\n”
+“\n”
+“\n”
+“页面标题\n”
+“\n”
+“正文{\n”
+“背景色:黑色;\n”
+“文本对齐:居中;\n”
+“颜色:白色;\n”
+字体系列:Arial、Helvetica、无衬线;\n
+“}\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“欢迎使用虚拟组织\n”
+“用户模块成功运行

\n” +“有关详细信息,请致电支持团队”

\n” +“\n” +“\n” +“\n” + ""; } }

因此,任何人都可以告诉我这些问题。

您的安全配置需要额外的配置。您需要inmemory的authenticationProvider:

 @Autowired
            public void configureInMemoryAuthentication(AuthenticationManagerBuilder auth) throws Exception
            {
                auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder.encode("admin@123#")).roles("ADMIN");
            }
和DAO authenticationProvider:

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

        return authProvider;

    }

您的安全配置需要额外的配置。您需要inmemory的authenticationProvider:

 @Autowired
            public void configureInMemoryAuthentication(AuthenticationManagerBuilder auth) throws Exception
            {
                auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder.encode("admin@123#")).roles("ADMIN");
            }
和DAO authenticationProvider:

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

        return authProvider;

    }

404在spring应用程序中的主要原因是由于异常或缺乏可用的依赖项而未加载上下文。将logger设置为调试模式,并在应用程序启动时检查日志。404在spring应用程序中,主要是由于异常或缺乏可用的依赖项而未加载上下文。将记录器设置为调试模式,并在应用程序启动时检查日志。