-1

現在、Spring アプリケーションにセキュリティを追加しようとしていますが、getall URL にアクセスするためのセキュリティ アクセスができるように、リクエストに資格情報を追加する方法を知りたいだけです。これは、クライアント アプリケーションのポスト コードです。

    public void save(String object) {
    try {
        final String URL = "http://localhost:8080/opject/getall";
        Gson g = new Gson();
        String jsonsStrinjg = g.toJson(object);
        String p = post(URL, jsonsStrinjg);
        if (p != null)
            JOptionPane.showMessageDialog(null,"Sucsess");
        else
            JOptionPane.showMessageDialog(null,"Fail");


    }catch (Exception e){
        JOptionPane.showMessageDialog(null,e.getMessage());
    }
}

public String post(final String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    try(Response response= client.newCall(request).execute()){
        return response.body().string();

    }

}

これは、セキュリティを処理するサーバー側のコードです

@Configuration
@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {


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

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.POST,"**/getall").hasRole("ADMIN")
                .and()
                .csrf().disable()
                .formLogin().disable();
}
@Bean
public PasswordEncoder encoder(){

    return  new BCryptPasswordEncoder();


}

それで、パスワードとユーザー名を追加できる本文または要求に追加する必要があるものはありますか。

4

1 に答える 1