Skip to content

Commit d5ba71d

Browse files
committed
8_12_add_rest_security
1 parent b852018 commit d5ba71d

6 files changed

Lines changed: 62 additions & 9 deletions

File tree

config/curl.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
> For windows use `Git Bash`
33
44
#### get All Users
5-
`curl -s http://localhost:8080/topjava/rest/admin/users`
5+
`curl -s http://localhost:8080/topjava/rest/admin/users --user admin@gmail.com:admin`
66

77
#### get Users 100001
8-
`curl -s http://localhost:8080/topjava/rest/admin/users/100001`
8+
`curl -s http://localhost:8080/topjava/rest/admin/users/100001 --user admin@gmail.com:admin`
99

1010
#### get All Meals
11-
`curl -s http://localhost:8080/topjava/rest/profile/meals`
11+
`curl -s http://localhost:8080/topjava/rest/profile/meals --user user@yandex.ru:password`
1212

1313
#### get Meals 100003
14-
`curl -s http://localhost:8080/topjava/rest/profile/meals/100003`
14+
`curl -s http://localhost:8080/topjava/rest/profile/meals/100003 --user user@yandex.ru:password`
1515

1616
#### filter Meals
17-
`curl -s "http://localhost:8080/topjava/rest/profile/meals/filter?startDate=2020-01-30&startTime=07:00:00&endDate=2020-01-31&endTime=11:00:00"`
17+
`curl -s "http://localhost:8080/topjava/rest/profile/meals/filter?startDate=2020-01-30&startTime=07:00:00&endDate=2020-01-31&endTime=11:00:00" --user user@yandex.ru:password`
1818

1919
#### get Meals not found
20-
`curl -s -v http://localhost:8080/topjava/rest/profile/meals/100008`
20+
`curl -s -v http://localhost:8080/topjava/rest/profile/meals/100008 --user user@yandex.ru:password`
2121

2222
#### delete Meals
23-
`curl -s -X DELETE http://localhost:8080/topjava/rest/profile/meals/100002`
23+
`curl -s -X DELETE http://localhost:8080/topjava/rest/profile/meals/100002 --user user@yandex.ru:password`
2424

2525
#### create Meals
26-
`curl -s -X POST -d '{"dateTime":"2020-02-01T12:00","description":"Created lunch","calories":300}' -H 'Content-Type:application/json;charset=UTF-8' http://localhost:8080/topjava/rest/profile/meals`
26+
`curl -s -X POST -d '{"dateTime":"2020-02-01T12:00","description":"Created lunch","calories":300}' -H 'Content-Type:application/json;charset=UTF-8' http://localhost:8080/topjava/rest/profile/meals --user user@yandex.ru:password`
2727

2828
#### update Meals
29-
`curl -s -X PUT -d '{"dateTime":"2020-01-30T07:00", "description":"Updated breakfast", "calories":200}' -H 'Content-Type: application/json' http://localhost:8080/topjava/rest/profile/meals/100003`
29+
`curl -s -X PUT -d '{"dateTime":"2020-01-30T07:00", "description":"Updated breakfast", "calories":200}' -H 'Content-Type: application/json' http://localhost:8080/topjava/rest/profile/meals/100003 --user user@yandex.ru:password`

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1818
<spring.version>5.3.39</spring.version>
19+
<spring.security.version>5.8.16</spring.security.version>
1920
<spring-data-jpa.version>2.7.18</spring-data-jpa.version>
2021
<jackson.version>2.21.2</jackson.version>
2122
<tomcat.version>9.0.113</tomcat.version>
@@ -159,6 +160,18 @@
159160
<version>${spring-data-jpa.version}</version>
160161
</dependency>
161162

163+
<!--security-->
164+
<dependency>
165+
<groupId>org.springframework.security</groupId>
166+
<artifactId>spring-security-web</artifactId>
167+
<version>${spring.security.version}</version>
168+
</dependency>
169+
<dependency>
170+
<groupId>org.springframework.security</groupId>
171+
<artifactId>spring-security-config</artifactId>
172+
<version>${spring.security.version}</version>
173+
</dependency>
174+
162175
<!--- ORM -->
163176
<dependency>
164177
<groupId>org.hibernate</groupId>

src/main/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<logger name="ru.javawebinar.topjava" level="debug"/>
2424
<!--<logger name="org.springframework.web.servlet" level="debug"/>-->
2525
<logger name="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" level="debug"/>
26+
<logger name="org.springframework.security" level="debug"/>
2627

2728
<root level="info">
2829
<appender-ref ref="file"/>

src/main/resources/spring/spring-app.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</bean>
1111
-->
1212
<import resource="spring-cache.xml"/>
13+
<import resource="spring-security.xml"/>
1314

1415
<context:component-scan base-package="ru.javawebinar.**.service"/>
1516
</beans>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<beans:beans xmlns="http://www.springframework.org/schema/security"
2+
xmlns:beans="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5+
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
6+
7+
<http pattern="/resources/**" security="none"/>
8+
<http pattern="/webjars/**" security="none"/>
9+
10+
<http pattern="/rest/**" use-expressions="true" name="restSecurityFilterChain" create-session="stateless">
11+
<http-basic/>
12+
<intercept-url pattern="/rest/admin/**" access="hasRole('ADMIN')"/>
13+
<intercept-url pattern="/**" access="isAuthenticated()"/>
14+
<csrf disabled="true"/>
15+
</http>
16+
17+
<beans:bean name="noopEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder"/>
18+
19+
<authentication-manager>
20+
<authentication-provider>
21+
<password-encoder ref="noopEncoder"/>
22+
<user-service>
23+
<user name="user@yandex.ru" password="password" authorities="ROLE_USER"/>
24+
<user name="admin@gmail.com" password="admin" authorities="ROLE_ADMIN"/>
25+
</user-service>
26+
</authentication-provider>
27+
</authentication-manager>
28+
</beans:beans>

src/main/webapp/WEB-INF/web.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
<url-pattern>/</url-pattern>
3838
</servlet-mapping>
3939

40+
<!-- Spring Security -->
41+
<filter>
42+
<filter-name>springSecurityFilterChain</filter-name>
43+
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
44+
</filter>
45+
<filter-mapping>
46+
<filter-name>springSecurityFilterChain</filter-name>
47+
<url-pattern>/*</url-pattern>
48+
</filter-mapping>
49+
4050
<filter>
4151
<filter-name>encodingFilter</filter-name>
4252
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

0 commit comments

Comments
 (0)