>source

웹스톰에서 앵귤러 클라이언트를 생성했고 Eclipse에 Spring Boot 서버가 있습니다. 문제는 세션을 열려고 할 때 다음 오류 메시지가 표시된다는 것입니다.

OPTIONS http://localhost:8080/allquestions 401 ()
(anonymous) @ angular.js:12410
p @ angular.js:12155
(anonymous) @ angular.js:11908
(anonymous) @ angular.js:16648
$eval @ angular.js:17972
$digest @ angular.js:17786
$apply @ angular.js:18080
(anonymous) @ angular.js:19924
f @ angular.js:6111
(anonymous) @ angular.js:6390
XMLHttpRequest cannot load http://localhost:8080/allquestions. Response for preflight has invalid HTTP status code 401

웹 구성

@EnableWebMvc
@Configuration
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
public WebConfig() {
    super();
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");
    registry.addViewController("/").setViewName("welcome");
    registry.addViewController("/welcome").setViewName("welcome");
    registry.addViewController("/exam").setViewName("exam");
    registry.addViewController("/report").setViewName("report");
    registry.addViewController("/error").setViewName("error");
    registry.addViewController("/editing").setViewName("editing");
}
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    super.addResourceHandlers(registry);
    registry.addResourceHandler("/images/**").addResourceLocations("/images/");
    registry.addResourceHandler("/css/**").addResourceLocations("/css/");
    registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}

컨트롤러의 일부:

@RestController
public class HomeController {
@Autowired
private BusinessModel businessModel;
private List<String> messages;
private User currentUser;
@PostConstruct
public void init() {
    messages= businessModel.getMessages();
}
//Communicate informations to client
private void sendOptions(HttpServletResponse response) {
    if (businessModel.getCorsNeeded().booleanValue()) {
        //Fix the header CORS
        response.addHeader("Access-Control-Allow-Origin", "*");
        //Header Authorization
        response.addHeader("Access-Control-Allow-Headers", "Authorization");
    }
}
//questions list via OPTIONS
@RequestMapping(value= "/allquestions", method= RequestMethod.OPTIONS)
public void getAllQuestions(HttpServletResponse response) {
    sendOptions(response);
}

SecurityConfig:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UsersService usersService;
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic();
    http.authorizeRequests().antMatchers("/login").permitAll().antMatchers("/**").hasRole("USER").and().formLogin()
            .loginPage("/login").and().logout();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(usersService).passwordEncoder(new BCryptPasswordEncoder());
}
}

xml 파일을 추가할 수 없습니다(작업 컨텍스트 제한)...

나는 완전히 차단되었으며 당신의 도움이 필요합니다!

  • 이전 javascript : 자바 스크립트에서 ajax의 속도를 높이는 방법
  • 다음 reactjs : In React: JSX code(useRef 사용) 내의 ref 문에 화살표 함수를 넣으면 무엇을 얻을 수 있습니까?