Skip to content

Commit e503288

Browse files
committed
Add rest template logic
1 parent a7b463a commit e503288

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-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,52 @@
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+
return false;
27+
}
28+
29+
// 1件検索用メソッド
30+
@Override
31+
public User selectOne(String userId) {
32+
return null;
33+
}
34+
35+
// 全件検索用メソッド
36+
@Override
37+
public List<User> selectMany() {
38+
return null;
39+
}
40+
41+
// 1件更新用メソッド
42+
@Override
43+
public boolean update(User user) {
44+
return false;
45+
}
46+
47+
// 1件削除用メソッド
48+
@Override
49+
public boolean delete(String userId) {
50+
return false;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo.login.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
import com.example.demo.domain.service.RestService;
7+
8+
9+
@RestController
10+
public class UserRestController {
11+
@Autowired
12+
RestService service;
13+
}

0 commit comments

Comments
 (0)