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

SpringCORS和角不工作:HTTP状态代码403错误

越朗
2023-03-14
@Injectable()
export class LoginService {
  constructor(private http: Http) {}

  sendCredential(username: string, password: string) {
    const url = 'http://localhost:8181/token';
    const encodedCredential = username + ':' + password;
    const basicHeader = 'Basic ' + btoa(encodedCredential);
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-wwww-form-urlencoded');
    headers.append('Authorization' ,  basicHeader);
    const opts = new RequestOptions({headers: headers});
    return this.http.get(url, opts);
  }

(spring)SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private static final String[] PUBLIC_MATCHERS = {
            "/css/**",
            "/js/**",
            "/image/**",
            "/book/**",
            "/user/**"
    };

@Override
    protected void configure(HttpSecurity http) throws Exception{
        http
                .cors().and()
                .csrf().disable()
                .httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers(PUBLIC_MATCHERS)
                .permitAll()
                .anyRequest()
                .authenticated();
    }
 @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET","POST","DELETE","PUT","OPTIONS"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());
    }

LoginController.java

@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/token")
    public Map<String, String> token(HttpSession session, HttpServletRequest request) {
        String remoteHost = request.getRemoteHost();
        int portNumber = request.getRemotePort();
        String remoteAddr = request.getRemoteAddr();

        System.out.println(remoteHost + ":" + portNumber);
        System.out.println(remoteAddr);


        return Collections.singletonMap("token", session.getId());
    }
}

共有1个答案

郎聪
2023-03-14
@Bean
CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
        configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Origin", "Content-Type", "Accept", "Authorization"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
 类似资料:
  • 我得到一个AWS错误代码:SignatureDoesNotMatch,状态代码:403时,试图通过亚马逊SES发送邮件。 我已经确认我正在使用通过https://console.aws.amazon.com/iam/home?#users创建的正确凭据,错误仍然存在。 我假设我在iam/home上创建的凭据实际上是全局的,但我不知道我进一步做错了什么。整个错误是: AWS错误代码:Signatur

  • 此外,小于-1的状态代码被归一化为零。-1通常表示请求被中止,例如使用config.timeout 这同样也适用于新版本的Angular吗?我使用的是Angular 4.0.1。

  • 我正在尝试从站点获取数据。当我在Intellij IDEA中使用此代码时,一切正常,但当我在Android Studio和real device中使用此代码时,我得到: org.jsoup.HttpStatusException:获取URL的HTTP错误。状态=403 这是我的代码: 我找到的所有信息都是关于“userAgent()”方法的,但它没有帮助。 UPD:对不起,这是我的问题。正确的网址

  • 本文向大家介绍HTTP 状态码 400、401、403 的含义?相关面试题,主要包含被问及HTTP 状态码 400、401、403 的含义?时的应答技巧和注意事项,需要的朋友参考一下 参考回答: (1)400状态码:请求无效 产生原因: 前端提交数据的字段名称和字段类型与后台的实体没有保持一致 前端提交到后台的数据应该是json字符串类型,但是前端没有将对象JSON.stringify转化成字符串

  • 问题内容: 我现在将脚本更改为jquery。这是我的旧javascript: 我现在想将其更改为jquery-ajax: 像在旧脚本中一样,如何测试请求返回的状态码(和responseText)? 问题答案: 试试这个: 要么: