Встроенный экран входа в систему, появляющийся при запросе RestController oauth2 - PullRequest
0 голосов
/ 08 мая 2020

Я начинаю изучать OAth2. Это Код

AuthServerConfiguration

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthServerConfiguration extends AuthorizationServerConfigurerAdapter {

@Value("${user.oauth.clientId}")
private String clientID;

@Value("${user.oauth.clientSecret}")
private String clientSecret;

@Value("${user.oauth.redirectUris}")
private String redirectURLs;

@Value("${user.oauth.accessTokenValidity}")
private int accessTokenValidity;

@Value("${user.oauth.refreshTokenValidity}")
private int refreshTokenValidity;

@Autowired
private BCryptPasswordEncoder passwordEncoder;

@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    oauthServer.tokenKeyAccess("permitAll()")
        .checkTokenAccess("isAuthenticated()");
}

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients
        .inMemory()
        .withClient(clientID)
        .secret(passwordEncoder.encode(clientSecret))
        .authorizedGrantTypes("password", "authorization_code", "refresh_token")
        .scopes("user_info")
        .authorities("READ_ONLY_CLIENT")
        .redirectUris(redirectURLs)
        .accessTokenValiditySeconds(accessTokenValidity)
        .refreshTokenValiditySeconds(refreshTokenValidity);
}
}

ResourceServerConfig

@Configuration
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/api/**").authenticated()
        .antMatchers("/").permitAll();
}
}

SecurityConfiguration

@Configuration
@Order(1)
public class OauthSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Value("${user.oauth.user.username}")
private String username;

@Value("${user.oauth.user.password}")
private String password;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .antMatcher("/**")
        .authorizeRequests()
        .antMatchers("/oauth/authorize**", "/login**", "/error**")
        .permitAll()
        .and()
        .authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .formLogin().permitAll();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
        .withUser(username).password(passwordEncoder().encode(password)).roles("ADMIN");
}

@Bean
public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}
}

RestController

@RestController
@RequestMapping("/api/customers")
public class CustomerController {

@GetMapping("/customer/{userId}")
public CustomerData getCustomerProfile(@PathVariable("userId") String userId) {
  return getCustomer(userId);
}

private CustomerData getCustomer(final String userId) {
CustomerData customer = new CustomerData();
customer.setEmail("aaa@gmail.com");
customer.setFirstName("Demo");
customer.setLastName("User");
customer.setAge(21);
customer.setId(userId);
return customer;
}
}

И я помещаю аннотацию @ EnableResourceServer в основной класс.

Я могу генерировать код авторизации и accessToken. когда я запрашиваю контроллер у почтальона с помощью accessToken, возвращается встроенная страница входа html. не информация о клиенте. что мне здесь не хватает.

Это мои зависимости.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.4.0.RELEASE</version>
    </dependency>
</dependencies>

Это ответ, который я получаю от почтальона

...