将我的用户连接到我的 SPRING/ANGULAR 应用程序时出现问题

Problem connecting my user to my SPRING/ANGULAR application

提问人:GotaKev 提问时间:11/15/2023 更新时间:11/16/2023 访问量:24

问:

下午好

我目前是弹簧(后端)和角度(前端)应用程序的新手。我在应用程序的用户连接部分,我几乎完成了,但我还有最后一个根本无法解决的问题。

事实上,当我在表单中输入标识符时,它会将它们考虑在内,但我的印象是,即使我在背面的安全配置文件中授权访问,背面也拒绝让我通过!

这是检查过程中的错误:

core.mjs:11483 ERROR HttpErrorResponse error : {error: SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON at JSON.parse (<anonymous>…, text: '<!DOCTYPE html>\n<html lang="en">\n <head>\n <met…n in</button>\n </form>\n</div>\n</body></html>'} headers : HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ} message : "Http failure during parsing for http://localhost:7070/login" name : "HttpErrorResponse" ok : false status : 200 statusText : "OK" url : "http://localhost:7070/login" [[Prototype]] : HttpResponseBase constructor : class HttpErrorResponse [[Prototype]] : Object

这是我在春天的安全配置:

    `package com.tracker.EsportTracker.config;
    import java.util.Arrays;
    import java.util.Collections;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.http.HttpMethod;
    import org.springframework.security.config.Customizer;
    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.WebSecurityConfiguration;
    import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    import org.springframework.web.filter.CorsFilter;
    import org.springframework.security.web.SecurityFilterChain;
    import org.springframework.security.web.header.writers.StaticHeadersWriter;
    import com.tracker.EsportTracker.service.impl.AppUserDetailsService;

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig {
@Autowired
private AppUserDetailsService appUserDetailsService;

@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;

public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(appUserDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
public void configure(HttpSecurity http) throws Exception {
    http.csrf(csrf -> csrf.disable()).cors(Customizer.withDefaults())
            .authorizeHttpRequests(ar -> ar.requestMatchers("/users").permitAll())
            .authorizeHttpRequests(ar -> ar.anyRequest().authenticated())
            .formLogin(formLogin -> formLogin
                    .permitAll());
}
@Bean
public FilterRegistrationBean simpleCorsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.setAllowedOrigins(Arrays.asList("http://localhost:4200","http://localhost:4200/"));
    config.setAllowedMethods(Collections.singletonList("*"));
    config.setAllowedHeaders(Collections.singletonList("*"));
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
    return bean;
}

}`

总而言之,在前端,我调用 localhost:7070/users 以获取用户列表,然后我被重定向到 localhost:7070/login

提前致谢

弹簧 连接

评论


答:

0赞 ALPHA 11/16/2023 #1

试试这个: 在您的 SecurityConfig

@Bean 
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception
{
    http.csrf(csrf -> csrf.disable())
        .cors(Customizer.withDefaults())
        .authorizeHttpRequests( ar -> 
                ar.requestMatchers("/login").permitAll()
                ar.requestMatchers("/users").permitAll()
                ar.anyRequest().authenticated()
         ).formLogin(Customizer.withDefaults());
    return http.build()

}

评论

0赞 ALPHA 11/16/2023
你可以在这里学到很多关于 Spring 安全性的信息:docs.spring.io/spring-security/reference/servlet/authentication/...
0赞 GotaKev 11/16/2023
它仍然不起作用,我有同样的错误