로그인 (signin)

jb kim·2022년 3월 6일
0

REST API 블로그 앱

목록 보기
49/65

SecurityConfig

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

LoginDto

package com.blog.app.payload;

import lombok.Data;

@Data
public class LoginDto {
	private String usernameOrEmail;
	private String password;
}

AuthController

@RestController
@RequestMapping("/api/auth")
public class AuthController {

	@Autowired
	private AuthenticationManager authenticationManager;
	
	@PostMapping("/signin")
	public ResponseEntity<String> authenticateUser(@RequestBody LoginDto loginDto){
		Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
				loginDto.getUsernameOrEmail(), loginDto.getPassword()));
		
		SecurityContextHolder.getContext().setAuthentication(authentication);
		return new ResponseEntity<>("성공적으로 로그인!", HttpStatus.OK);		
	}
}

SecurityConfig

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		http
			.csrf().disable()
			.authorizeRequests()
			.antMatchers(HttpMethod.GET, "/api/**").permitAll()
			.antMatchers("/api/auth/**").permitAll()
			.anyRequest()
			.authenticated()
			.and()
			.httpBasic(); //베이직 인증창
	}

테스트

참고
https://kogle.tistory.com/31
https://godekdls.github.io/Spring%20Security/authentication/

profile
픽서

0개의 댓글