当执行身份验证的post请求时,会发出错误404 not found

p8ekf7hl  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(198)

在执行 POST 请求身份验证时,发出错误。错误发生在我开始使用cors请求时,表示我从提供商处购买的静态ip。
如果我不使用这个ip(但指定localhost),那么请求将正确执行。spring启动应用程序。
请求:

POST http://81.211.111.41:1414/auth/login (IP and port are for example)

body 

  {
  "username":"jeffryus",
  "password":"1234567890"
  }

控制器:

@RestController
@RequestMapping(value="/auth/")
public class UserAuthenticationController {

    @Autowired
    private  JwtUserService jwtUserService;

    @Autowired 
    AuthenticationManager authenticationManager;

    @CrossOrigin
    @RequestMapping(value = { "login" }, method = RequestMethod.POST)
    public ResponseEntity<?> authUser(@RequestBody  AuthenticationRequestDto auth) {
        return jwtUserService.login(auth);
    }

    @CrossOrigin
    @RequestMapping(value = { "signup" }, method = RequestMethod.POST)
    public ResponseEntity<?> registerUser(@RequestBody SignupRequest user) {
        return  jwtUserService.register(user);
    }

配置:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String LOGIN_ENDPOINT = "/auth/**";

    @Autowired
    private  JwtTokenProvider jwtTokenProvider;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtUserService jwtUserService;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http 
            .cors()
                .and()
            .httpBasic().disable()
            .csrf().disable()
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
            .authorizeRequests()
                .antMatchers(LOGIN_ENDPOINT).permitAll()    
                .anyRequest().authenticated()
                .and()
            .apply(new JwtConfigurer(jwtTokenProvider));
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://81.211.111.41:1414"));
        //or any domain that you want to restrict to 
        configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT"));
        //Add the method support as you like
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(jwtUserService)
            .passwordEncoder(passwordEncoder);
    }
}

特性:

server.port=1414

spring.datasource.url=jdbc:*******
spring.datasource.username=*******
spring.datasource.password=******
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=validate

jwt.token.secret=abc
jwt.token.expired=3600000

发送请求时 Postman 出错:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>

<head>
    <title>404 Not Found</title>
</head>

<body>
    <h1>Not Found</h1>
    <p>The requested URL was not found on this server.</p>
</body>

</html>

如果我破坏了请求中的语法,就会出现这样的错误。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题