こんにちは、
Tomcat にデプロイされたサーバーにデータを POST しようとすると、エラーが発生します。それはSpringのセキュリティの問題だと思います。
サーバー上での私のセキュリティ設定は次のとおりです。
try {
http.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.antMatchers("/mainMenu/admin/*").access("hasRole('" + ENUM_USER_ROLES.ADMIN.getValue() +"')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')")
.antMatchers(HttpMethod.GET,"/api/mobile/**").authenticated()
.antMatchers(HttpMethod.POST,"/api/mobile/**").authenticated()
// .antMatchers(HttpMethod.POST, "/api/mobile/**").access("hasRole('COMPANY_MOBILE')")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
http.httpBasic();
} catch(Exception e) {
logger.error(e.getMessage());
}
コントローラーには、2 つのメソッドがあります。
@RequestMapping(value = "/api/mobile/test/1", method=RequestMethod.GET, produces={"application/json"})
public @ResponseBody String populateActivePSwapBasketGET() {
return "HELLO get";
}
@RequestMapping(value = "/api/mobile/test/2", method=RequestMethod.POST, produces={"application/json"})
public @ResponseBody String populateActivePSwapBasketPOST() {
return "HELLO post";
}
私のアンドロイドの観点から、私は最初のGETメソッドを正常に呼び出していますが、POSTメソッドは
org.springframework.web.client.HttpClientErrorException: 403 禁止
Android フォンでは、サーバーで Get メソッドと Post メソッドを呼び出す 2 つの方法を次に示します。2番目は私に例外を与えます:
HttpAuthentication authHeader = new HttpBasicAuthentication(userName, password);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
final Gson gson = new Gson();
// Create the request body as a MultiValueMap
MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
RestTemplate restTemplate = new RestTemplate(true);
try {
ResponseEntity<String> out1 = restTemplate.exchange(
AndroidPhoneProperties.REST_API + "/api/mobile/test/1",
HttpMethod.GET,
requestEntity,
String.class);
ResponseEntity<String> out2 = restTemplate.exchange(
AndroidPhoneProperties.REST_API + "/api/mobile/test/2",
HttpMethod.POST,
requestEntity,
String.class);
String asd = "";
} catch (HttpClientErrorException e) {
CustomAppLogging.e(this.getClass().getName(), CLASSNAME + " HttpClientErrorException " + e.getMessage());
}
サーバー用の私のgradleファイルは次のとおりです。
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework:spring-jdbc:4.1.0.RELEASE")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("mysql:mysql-connector-java:5.1.+")
compile("org.webjars:bootstrap:3.0.3")
compile("org.webjars:jquery:2.0.3-1")
compile("org.springframework.security.oauth:spring-security-oauth2:2.0.7.RELEASE")
compile("org.thymeleaf:thymeleaf-spring4")
compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")
compile("org.springframework.boot:spring-boot-starter-web")
compile("com.google.code.gson:gson:2.2.4")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testCompile("junit:junit")
}
そして、ここに私のAndroidプロジェクトのGradleファイルがあります:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1'
compile 'com.google.code.gson:gson:2.3'
compile 'org.springframework.android:spring-android-rest-template:2.0.0.M1'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:support-v4:22.1.1'
compile 'de.hdodenhof:circleimageview:1.3.0'
}
これをテストしているときは、次のようにコマンドラインから実行しています。
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar build/libs/xxx-0.1.0.jar
POST が拒否された理由に関する詳細情報を教えてくれる Spring セキュリティのログはどこで確認できますか? Tomcat サーバーにデプロイするように切り替えた場合、より良いログを取得できますか?
さらに情報が必要な場合は、お尋ねください。
ご協力いただきありがとうございます。