Skip to content

Commit 355fc40

Browse files
committed
Use Spring Security's authorities instead of roles.
No functional changes.
1 parent 03c8ef9 commit 355fc40

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

src/main/java/ru/mystamps/web/service/CountryServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public CountryServiceImpl(CountryDao countryDao) {
4646

4747
@Override
4848
@Transactional
49-
@PreAuthorize("hasAuthority('ROLE_USER')")
49+
@PreAuthorize("hasAuthority('CREATE_COUNTRY')")
5050
public Country add(AddCountryDto dto, User user) {
5151
Validate.isTrue(dto != null, "DTO should be non null");
5252
Validate.isTrue(dto.getName() != null, "Country name should be non null");

src/main/java/ru/mystamps/web/service/SeriesServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public SeriesServiceImpl(SeriesDao seriesDao, ImageService imageService) {
5959

6060
@Override
6161
@Transactional
62-
@PreAuthorize("hasAuthority('ROLE_USER')")
62+
@PreAuthorize("hasAuthority('CREATE_SERIES')")
6363
public Series add(AddSeriesDto dto, User user) {
6464
Validate.isTrue(dto != null, "DTO must be non null");
6565
Validate.isTrue(dto.getQuantity() != null, "Stamps quantity must be non null");

src/main/java/ru/mystamps/web/support/spring/security/CustomUserDetailsService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
package ru.mystamps.web.support.spring.security;
1919

2020
import java.util.Collection;
21-
import java.util.Collections;
21+
import java.util.LinkedList;
22+
import java.util.List;
2223

2324
import javax.inject.Inject;
2425

@@ -70,7 +71,10 @@ public UserDetails loadUserByUsername(String login) {
7071
}
7172

7273
private static Collection<? extends GrantedAuthority> getAuthorities() {
73-
return Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"));
74+
List<SimpleGrantedAuthority> authorities = new LinkedList<SimpleGrantedAuthority>();
75+
authorities.add(new SimpleGrantedAuthority("CREATE_COUNTRY"));
76+
authorities.add(new SimpleGrantedAuthority("CREATE_SERIES"));
77+
return authorities;
7478
}
7579

7680
}

src/main/resources/spring/security.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
<access-denied-handler error-page="/error/401" />
3232

33-
<intercept-url pattern="/country/add" access="hasRole('ROLE_USER')" />
34-
<intercept-url pattern="/series/add" access="hasRole('ROLE_USER')" />
33+
<intercept-url pattern="/country/add" access="hasAuthority('CREATE_COUNTRY')" />
34+
<intercept-url pattern="/series/add" access="hasAuthority('CREATE_SERIES')" />
3535

3636
<intercept-url pattern="/**" access="permitAll" />
3737
</http>

src/main/webapp/WEB-INF/tiles/body/site/index.jsp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
<spring:url var="addSeriesUrl" value="<%= Url.ADD_SERIES_PAGE %>" />
55
<spring:url var="addCountryUrl" value="<%= Url.ADD_COUNTRY_PAGE %>" />
66

7-
<sec:authorize access="hasRole('ROLE_USER')">
7+
<sec:authorize access="hasAnyAuthority('CREATE_COUNTRY', 'CREATE_SERIES')">
88
<spring:message code="t_you_may" />:
99
<nav>
1010
<ul>
11-
<li><a href="${addSeriesUrl}"><spring:message code="t_add_series" /></a></li>
12-
<li><a href="${addCountryUrl}"><spring:message code="t_add_country" /></a></li>
11+
<sec:authorize access="hasAuthority('CREATE_SERIES')">
12+
<li><a href="${addSeriesUrl}"><spring:message code="t_add_series" /></a></li>
13+
</sec:authorize>
14+
<sec:authorize access="hasAuthority('CREATE_COUNTRY')">
15+
<li><a href="${addCountryUrl}"><spring:message code="t_add_country" /></a></li>
16+
</sec:authorize>
1317
</ul>
1418
</nav>
1519
</sec:authorize>

0 commit comments

Comments
 (0)