File tree 5 files changed +131
-0
lines changed
src/main/java/com/example/demo
5 files changed +131
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 13
13
import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
14
14
import org .springframework .security .crypto .password .PasswordEncoder ;
15
15
import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
16
+ import org .springframework .security .web .util .matcher .RequestMatcher ;
16
17
17
18
18
19
@ EnableWebSecurity
@@ -62,6 +63,7 @@ protected void configure(HttpSecurity http) throws Exception {
62
63
.antMatchers ("/css/**" ).permitAll () // cssへのアクセス許可
63
64
.antMatchers ("/login" ).permitAll () // ログインページは直リンク許可
64
65
.antMatchers ("/signup" ).permitAll () // ユーザー登録画面は直リンク許可
66
+ .antMatchers ("/rest/**" ).permitAll () // REST
65
67
.antMatchers ("/admin" ).hasAuthority ("ROLE_ADMIN" ) // 権限の設定
66
68
.anyRequest ().authenticated (); // それ以外は直リンク禁止
67
69
@@ -80,6 +82,12 @@ protected void configure(HttpSecurity http) throws Exception {
80
82
.logoutUrl ("/logout" )
81
83
.logoutSuccessUrl ("/login" );
82
84
85
+ // CSRFを無効にするURLを設定
86
+ RequestMatcher csrfMatcher = new RestMatcher ("/rest/**" );
87
+
88
+ // RESTのみCSRF対策を無効に設定
89
+ http .csrf ().requireCsrfProtectionMatcher (csrfMatcher );
90
+
83
91
// CSRF対策を無効に設定(一時的)
84
92
// http.csrf().disable();
85
93
}
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments