Skip to content

Commit 1e7a0fe

Browse files
committed
lab05 spring security simplest jdbc edition.
1 parent 694c723 commit 1e7a0fe

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

lab05_spring5mvc_sec_jdbc/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,63 @@
2121

2222
基于: https://www.mkyong.com/spring-security/spring-security-hello-world-example/
2323

24+
```xml
25+
<beans:beans xmlns="http://www.springframework.org/schema/security"
26+
xmlns:beans="http://www.springframework.org/schema/beans"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://www.springframework.org/schema/beans
29+
http://www.springframework.org/schema/beans/spring-beans.xsd
30+
http://www.springframework.org/schema/security
31+
http://www.springframework.org/schema/security/spring-security-4.2.xsd">
32+
33+
<http auto-config="true" use-expressions="true">
34+
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
35+
</http>
36+
37+
<authentication-manager>
38+
<authentication-provider>
39+
<user-service>
40+
<user name="cyper" password="{noop}123456" authorities="ROLE_ADMIN" />
41+
</user-service>
42+
</authentication-provider>
43+
</authentication-manager>
44+
45+
</beans:beans>
46+
```
47+
2448
默认提供的 EndPoint 有:
2549

2650
1. GET/POST http://localhost:8080/course-app/login
2751
2. GET/POST http://localhost:8080/course-app/logout
2852

53+
### Security jdbc version
54+
55+
```xml
56+
<http auto-config="true" use-expressions="true">
57+
<csrf disabled="true"/>
58+
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
59+
</http>
60+
61+
<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method="getInstance"/>
62+
63+
<authentication-manager>
64+
<authentication-provider>
65+
<!-- <user-service>
66+
<user name="cyper" password="{noop}123456" authorities="ROLE_ADMIN" />
67+
</user-service> -->
68+
69+
<password-encoder ref="passwordEncoder" />
70+
71+
<jdbc-user-service
72+
data-source-ref="dataSource"
73+
users-by-username-query="select username, password, 'true' from tbl_user where username=?;"
74+
authorities-by-username-query="select username, 'ROLE_ADMIN' from tbl_user where username=?;"
75+
/>
76+
77+
</authentication-provider>
78+
</authentication-manager>
79+
```
80+
2981
### Security Errors
3082

3183
1. Failed to evaluate expression 'ROLE_USER' => (ref #5)
@@ -42,3 +94,4 @@
4294
3. https://stackoverflow.com/questions/41577234/why-does-spring-mvc-respond-with-a-404-and-report-no-mapping-found-for-http-req
4395
4. https://howtodoinjava.com/spring5/security5/security-java-config-enablewebsecurity-example/
4496
5. https://stackoverflow.com/questions/35715065/it-throws-me-500-failed-to-evaluate-expression-role-user-in-spring-security
97+
6. [What is the difference between spring factory-method and factory-bean?](https://stackoverflow.com/questions/18772490/what-is-the-difference-between-spring-factory-method-and-factory-bean)

lab05_spring5mvc_sec_jdbc/course-app/src/main/resouces/spring/spring-security.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@
77
http://www.springframework.org/schema/security/spring-security-4.2.xsd">
88

99
<http auto-config="true" use-expressions="true">
10+
<csrf disabled="true"/>
1011
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
1112
</http>
13+
14+
<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method="getInstance"/>
1215

1316
<authentication-manager>
1417
<authentication-provider>
15-
<user-service>
18+
<!-- <user-service>
1619
<user name="cyper" password="{noop}123456" authorities="ROLE_ADMIN" />
17-
</user-service>
20+
</user-service> -->
21+
22+
<password-encoder ref="passwordEncoder" />
23+
24+
<jdbc-user-service
25+
data-source-ref="dataSource"
26+
users-by-username-query="select username, password, 'true' from tbl_user where username=?;"
27+
authorities-by-username-query="select username, 'ROLE_ADMIN' from tbl_user where username=?;"
28+
/>
29+
1830
</authentication-provider>
1931
</authentication-manager>
2032

0 commit comments

Comments
 (0)