폼인증 설정 지우기
의존성 추가
httpBasic 메소드 등의 인증 테스트를 하기위함
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>5.1.4.RELEASE</version>
<scope>test</scope>
</dependency>
public class AuthServerConfigTest extends BaseControllerTest {
@Autowired
AccountService accountService;
@Test
@TestDescription("인증 토큰을 발급받는 테스트")
public void getAuthToken() throws Exception {
String clientId = "myApp";
String clientSecret = "secret";
Set<AccountRole> roles = new HashSet<>();
roles.add(AccountRole.ADMIN);
roles.add(AccountRole.USER);
String username = "[email protected]";
String password = "pass";
Account cmlee = Account.builder()
.email(username)
.password(password)
.roles(roles)
.build();
this.accountService.saveAccount(cmlee);
this.mockMvc.perform(post("/oauth/token")
.with(httpBasic(clientId, clientSecret))
.param("username", username)
.param("password", password)
.param("grant_type", "password"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("access_token").exists());
}
}
인증 서버 설정
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
PasswordEncoder passwordEncoder;
@Autowired
AuthenticationManager authenticationManager;
@Autowired
AccountService accountService;
@Autowired
TokenStore tokenStore;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.passwordEncoder(passwordEncoder);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// clients.jdbc()
clients.inMemory()
.withClient("myApp")
.authorizedGrantTypes("password", "refresh_token")
.scopes("read", "write")
.secret(this.passwordEncoder.encode("secret"))
.accessTokenValiditySeconds(10 * 60)
.refreshTokenValiditySeconds(6 * 10 * 60);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(accountService)
.tokenStore(tokenStore);
}
}