Skip to content

Commit f58dafe

Browse files
authored
Merge pull request #13 from kento-kotlin-sandbox/future/add_rest_logic
Future/add rest logic
2 parents a7b463a + c9ddcce commit f58dafe

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.demo;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
5+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
6+
import org.springframework.security.web.util.matcher.RequestMatcher;
7+
8+
9+
public class RestMatcher implements RequestMatcher {
10+
11+
// マッチャー
12+
private AntPathRequestMatcher matcher;
13+
14+
// コンストラクタ
15+
public RestMatcher(String url) {
16+
super();
17+
matcher = new AntPathRequestMatcher(url);
18+
}
19+
20+
// URLのマッチ条件
21+
@Override
22+
public boolean matches(HttpServletRequest request) {
23+
// GETならCSRFのチェックをしない
24+
if("GET".equals(request.getMethod())) {
25+
return false;
26+
}
27+
28+
// 特定のURLに該当する場合、CSRFチェックしない
29+
if(matcher.matches(request)) {
30+
return false;
31+
}
32+
33+
return true;
34+
}
35+
}

src/main/java/com/example/demo/SecurityConfig.java

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1414
import org.springframework.security.crypto.password.PasswordEncoder;
1515
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
16+
import org.springframework.security.web.util.matcher.RequestMatcher;
1617

1718

1819
@EnableWebSecurity
@@ -62,6 +63,7 @@ protected void configure(HttpSecurity http) throws Exception {
6263
.antMatchers("/css/**").permitAll() // cssへのアクセス許可
6364
.antMatchers("/login").permitAll() // ログインページは直リンク許可
6465
.antMatchers("/signup").permitAll() // ユーザー登録画面は直リンク許可
66+
.antMatchers("/rest/**").permitAll() // REST
6567
.antMatchers("/admin").hasAuthority("ROLE_ADMIN") // 権限の設定
6668
.anyRequest().authenticated(); // それ以外は直リンク禁止
6769

@@ -80,6 +82,12 @@ protected void configure(HttpSecurity http) throws Exception {
8082
.logoutUrl("/logout")
8183
.logoutSuccessUrl("/login");
8284

85+
// CSRFを無効にするURLを設定
86+
RequestMatcher csrfMatcher = new RestMatcher("/rest/**");
87+
88+
// RESTのみCSRF対策を無効に設定
89+
http.csrf().requireCsrfProtectionMatcher(csrfMatcher);
90+
8391
// CSRF対策を無効に設定(一時的)
8492
// http.csrf().disable();
8593
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.demo.domain.service;
2+
3+
import java.util.List;
4+
5+
import com.example.demo.domain.model.User;
6+
7+
// Rest用インターフェース
8+
public interface RestService {
9+
// 1件登録用メソッド
10+
public boolean insert(User user);
11+
12+
// 1件検索用メソッド
13+
public User selectOne(String userId);
14+
15+
// 全件検索用メソッド
16+
public List<User> selectMany();
17+
18+
// 1件更新用メソッド
19+
public boolean update(User user);
20+
21+
// 1件削除用メソッド
22+
public boolean delete(String userId);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.example.demo.domain.service.jdbc;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import com.example.demo.domain.model.User;
11+
import com.example.demo.domain.model.repository.UserDao;
12+
import com.example.demo.domain.service.RestService;
13+
14+
15+
@Transactional
16+
@Service
17+
public class RestServiceJdbcImpl implements RestService {
18+
19+
@Autowired
20+
@Qualifier("UserDaoJdbcImpl")
21+
UserDao dao;
22+
23+
// 1件登録用メソッド
24+
@Override
25+
public boolean insert(User user) {
26+
int result = dao.insertOne(user);
27+
28+
if(result == 0) {
29+
return false;
30+
} else {
31+
return true;
32+
}
33+
}
34+
35+
// 1件検索用メソッド
36+
@Override
37+
public User selectOne(String userId) {
38+
return dao.selectOne(userId);
39+
}
40+
41+
// 全件検索用メソッド
42+
@Override
43+
public List<User> selectMany() {
44+
return dao.selectMany();
45+
}
46+
47+
// 1件更新用メソッド
48+
@Override
49+
public boolean update(User user) {
50+
int result = dao.updateOne(user);
51+
52+
if(result == 0) {
53+
return false;
54+
} else {
55+
return true;
56+
}
57+
}
58+
59+
// 1件削除用メソッド
60+
@Override
61+
public boolean delete(String userId) {
62+
int result = dao.deleteOne(userId);
63+
64+
if(result == 0) {
65+
return false;
66+
} else {
67+
return true;
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.example.demo.login.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.DeleteMapping;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.PutMapping;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import com.example.demo.domain.model.User;
15+
import com.example.demo.domain.service.RestService;
16+
17+
18+
@RestController
19+
public class UserRestController {
20+
@Autowired
21+
RestService service;
22+
23+
// ユーザー全件取得
24+
@GetMapping("/rest/get")
25+
public List<User> getUserMany() {
26+
// ユーザー全件取得
27+
return service.selectMany();
28+
}
29+
30+
// ユーザー1件取得
31+
@GetMapping("/rest/get/{id:.+}")
32+
public User getUserOne(@PathVariable("id") String userId) {
33+
// ユーザー1件取得
34+
return service.selectOne(userId);
35+
}
36+
37+
// ユーザー1件登録
38+
@PostMapping("/rest/insert")
39+
public String postUserOne(@RequestBody User user) {
40+
// ユーザーを1件登録
41+
boolean result = service.insert(user);
42+
43+
String str = "";
44+
45+
if(result) {
46+
str = "{\"result\":\"ok\"}";
47+
} else {
48+
str = "{\"result\":\"error\"}";
49+
}
50+
51+
// 結果用の文字列を返す
52+
return str;
53+
}
54+
55+
// ユーザー1件更新
56+
@PutMapping("/rest/update")
57+
public String putUserOne(@RequestBody User user) {
58+
// ユーザー1件更新
59+
boolean result = service.update(user);
60+
61+
String str = "";
62+
63+
if(result) {
64+
str = "{\"result\":\"ok\"}";
65+
} else {
66+
str = "{\"result\":\"error\"}";
67+
}
68+
69+
return str;
70+
}
71+
72+
// ユーザー1件削除
73+
@DeleteMapping("/rest/delete/{id:.+}")
74+
public String deleteUserOne(@PathVariable("id") String userId) {
75+
// ユーザー1件削除
76+
boolean result = service.delete(userId);
77+
78+
String str = "";
79+
80+
if(result) {
81+
str = "{\"result\":\"ok\"}";
82+
} else {
83+
str = "{\"result\":\"error\"}";
84+
}
85+
86+
// 結果用の文字列をリターン
87+
return str;
88+
}
89+
}

0 commit comments

Comments
 (0)