Ошибка 401 при втором вызове метода контроллера - PullRequest
0 голосов
/ 12 апреля 2019

В моем весеннем приложении я хочу вернуть некоторую информацию моему угловому клиенту.Сначала я отправляю запрос в '/ login', и это прекрасно работает.Затем я отправляю запрос HTTP-post в '/ user', и он также работает нормально.Но второй вызов '/ user' возвращает исключение 401.

У меня также есть XhrInterceptor в app.module.ts

Вот моя конфигурация безопасности:

@Configuration
@EnableWebSecurity
public class BasicAuthConfiguration extends WebSecurityConfigurerAdapter {

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

@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
    authenticationManagerBuilder
            .authenticationProvider(authenticationProvider());
  }

@Bean
public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
    authProvider.setUserDetailsService(userService);
    authProvider.setPasswordEncoder(getPasswordEncoder());

    return authProvider;
  }

@Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .antMatchers("/login").permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic();
    http.cors();
}

Controller.java

@RestController
@Api(tags = "user")
@CrossOrigin(value = "*", allowedHeaders = {"*"})
public class UserController {

  @Resource(name = "authenticationManager")
  private AuthenticationManager authManager;

  @RequestMapping("/login")
  public boolean login(@RequestParam("username") final String username, @RequestParam("password") final String password, final HttpServletRequest request) {
    UsernamePasswordAuthenticationToken authReq =
            new UsernamePasswordAuthenticationToken(username, password);
    Authentication auth = authManager.authenticate(authReq);
    SecurityContext sc = SecurityContextHolder.getContext();
    sc.setAuthentication(auth);
    HttpSession session = request.getSession(true);
    session.setAttribute("SPRING_SECURITY_CONTEXT", sc);
    return
            username.equals("john.doe") && password.equals("passwd");
  }

@RequestMapping(value = "/user")
  public Principal user(HttpServletRequest request) {
    String authToken = request.getHeader("Authorization")
            .substring("Basic".length()).trim();

    return () -> new String(Base64.getDecoder()
            .decode(authToken)).split(":")[0];
  }
}

AuthService.ts

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  constructor(private http: HttpClient, private router: Router) { }

userName: string;

auth() {
    const headers = new HttpHeaders({
      authorization: 'Basic ' + btoa('john.doe:passwd')
    });

    let url = 'http://localhost:8080/login';

    const formData = new FormData();
    formData.append("username", "john.doe")
    formData.append("password", "passwd")

    this.http.post(url, formData, { headers: headers }).subscribe(isValid => {
      if (isValid) {
        console.log("isValid", isValid);

        sessionStorage.setItem('token', btoa('john.doe:passwd'));
        this.router.navigate(['']);
      } else {
        alert("Authentication failed.")
      }
    });
  }


  getUser() {
    let url = 'http://localhost:8080/user';

    let headers: HttpHeaders = new HttpHeaders({
      'Authorization': 'Basic ' + sessionStorage.getItem('token')
    });

    let options = { headers: headers };


    // this.http.post(url, "johndoe").
    this.http.get(url, options).
      subscribe(principal => {
        console.log(principal);

        this.userName = principal['name'];
      },
        error => {
          if (error.status == 401)
            alert('Unauthorized');
        }
      );
  }

LoginComponent.ts

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  constructor(private authService: AuthService, private http: HttpClient,  
  private router: Router) {}

  ngOnInit() {
    sessionStorage.setItem('token', '');
    this.authService.auth()
  }
}

Ответы [ 2 ]

0 голосов
/ 15 апреля 2019

Раньше я не знал, что для использования httpBasic () всегда требуется аутентификация для каждого запроса.Поэтому я отправляю имя пользователя и пароль в каждом запросе в качестве заголовка авторизации.

0 голосов
/ 12 апреля 2019

ОБНОВЛЕНИЕ Вы можете добавить это в свой метод настройки:

   protected void configure(HttpSecurity http) throws Exception {
     http.csrf().disable()
             .authorizeRequests()
             .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
             .antMatchers("/login").permitAll()
             .antMatchers("/users").permitAll()
             .anyRequest()
             .authenticated()
             .and()
             .httpBasic();
     http.cors();
 } 

Вы хотите, чтобы Angular перехватил URL / user?Если это так, вы можете настроить ViewController, перенаправляя любой URL, который вы хотите, на index.html, который Angular читает

public void addViewControllers(ViewControllerRegistry registry) {
        String forward = "forward:/index.html";
        registry.addViewController("/").setViewName(forward);
        registry.addViewController("/login").setViewName(forward);
        registry.addViewController("/user").setViewName(forward);
    }

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...