Skip to content

Commit 6b59bed

Browse files
committed
added securing methods of services,
fix creation of user -- user have one role, ROLE_USER
1 parent 24d944b commit 6b59bed

File tree

12 files changed

+79
-35
lines changed

12 files changed

+79
-35
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
<artifactId>joda-time-hibernate</artifactId>
5757
<version>1.4</version>
5858
</dependency>
59+
<dependency>
60+
<groupId>com.google.guava</groupId>
61+
<artifactId>guava</artifactId>
62+
<version>19.0</version>
63+
</dependency>
5964
<!-- SMTP -->
6065
<dependency>
6166
<groupId>com.sun.mail</groupId>
@@ -120,6 +125,11 @@
120125
<artifactId>spring-security-web</artifactId>
121126
<version>4.2.1.RELEASE</version>
122127
</dependency>
128+
<dependency>
129+
<groupId>org.springframework.security</groupId>
130+
<artifactId>spring-security-taglibs</artifactId>
131+
<version>4.2.1.RELEASE</version>
132+
</dependency>
123133
<dependency>
124134
<groupId>org.springframework.security</groupId>
125135
<artifactId>spring-security-test</artifactId>
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.mrchebik.service;
22

3+
import org.springframework.security.access.prepost.PreAuthorize;
34
import ru.mrchebik.model.Category;
45

56
import java.util.List;
@@ -8,12 +9,24 @@
89
* Created by mrchebik on 14.01.17.
910
*/
1011
public interface CategoryService {
12+
@PreAuthorize("hasRole('ROLE_USER')")
1113
void add(Category category);
14+
15+
@PreAuthorize("hasRole('ROLE_USER')")
1216
void edit(String name, long categoryId);
17+
18+
@PreAuthorize("hasRole('ROLE_USER')")
1319
Category findById(long id);
20+
21+
@PreAuthorize("hasRole('ROLE_USER')")
1422
Category findByParentIdThroughCategoryId(long parentId, long userId);
23+
24+
@PreAuthorize("hasRole('ROLE_USER')")
1525
List<Category> findByParentId(long parentId, long userId);
26+
27+
@PreAuthorize("hasRole('ROLE_USER')")
1628
List<Category> findAll(long userId);
17-
long findMaxLevel(long userId);
29+
30+
@PreAuthorize("hasRole('ROLE_USER')")
1831
void remove(long id);
1932
}

src/main/java/ru/mrchebik/service/CommentService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.mrchebik.service;
22

3+
import org.springframework.security.access.prepost.PreAuthorize;
34
import ru.mrchebik.model.Comment;
45

56
import java.util.List;
@@ -8,9 +9,18 @@
89
* Created by mrchebik on 14.01.17.
910
*/
1011
public interface CommentService {
12+
@PreAuthorize("hasRole('ROLE_USER')")
1113
Comment addComment(Comment comment);
14+
15+
@PreAuthorize("hasRole('ROLE_USER')")
1216
void editComment(Comment comment);
17+
18+
@PreAuthorize("hasRole('ROLE_USER')")
1319
Comment findComment(long id);
20+
21+
@PreAuthorize("hasRole('ROLE_USER')")
1422
List<Comment> findComments(long id);
23+
24+
@PreAuthorize("hasRole('ROLE_USER')")
1525
void removeComment(long id);
1626
}

src/main/java/ru/mrchebik/service/PostService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.mrchebik.service;
22

3+
import org.springframework.security.access.prepost.PreAuthorize;
34
import ru.mrchebik.model.Post;
45

56
import java.util.List;
@@ -8,9 +9,18 @@
89
* Created by mrchebik on 14.01.17.
910
*/
1011
public interface PostService {
12+
@PreAuthorize("hasRole('ROLE_USER')")
1113
Post add(Post post);
14+
15+
@PreAuthorize("hasRole('ROLE_USER')")
1216
long findLastPostId(long userId);
17+
18+
@PreAuthorize("hasRole('ROLE_USER')")
1319
List<Post> findPosts(long userId);
20+
21+
@PreAuthorize("hasRole('ROLE_USER')")
1422
Post findPost(long postId);
23+
24+
@PreAuthorize("hasRole('ROLE_USER')")
1525
void remove(long id);
1626
}

src/main/java/ru/mrchebik/service/ReaderService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.mrchebik.service;
22

3+
import org.springframework.security.access.prepost.PreAuthorize;
34
import ru.mrchebik.model.Reader;
45

56
import java.util.List;
@@ -8,9 +9,18 @@
89
* Created by mrchebik on 03.02.17.
910
*/
1011
public interface ReaderService {
12+
@PreAuthorize("hasRole('ROLE_USER')")
1113
void add(Reader reader);
14+
15+
@PreAuthorize("hasRole('ROLE_USER')")
1216
Reader findOne(long userIdMain, long userIdFollower);
17+
18+
@PreAuthorize("hasRole('ROLE_USER')")
1319
List<Reader> findAllMain(long userIdMain);
20+
21+
@PreAuthorize("hasRole('ROLE_USER')")
1422
List<Reader> findAllFollower(long userIdMain);
23+
24+
@PreAuthorize("hasRole('ROLE_USER')")
1525
void delete(long id);
1626
}

src/main/java/ru/mrchebik/service/SecurityService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
* Created by mrchebik on 15.01.17.
55
*/
66
public interface SecurityService {
7-
String findLoggedInUsername();
87
void autologin(final String username, final String password);
98
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
package ru.mrchebik.service;
22

3+
import org.springframework.security.access.prepost.PreAuthorize;
34
import ru.mrchebik.model.User;
45

5-
import java.util.List;
6-
76
/**
87
* Created by mrchebik on 14.01.17.
98
*/
109
public interface UserService {
1110
User add(User user);
11+
12+
@PreAuthorize("hasRole('ROLE_USER')")
1213
void changeUsername(String email, String username);
14+
15+
@PreAuthorize("hasRole('ROLE_USER')")
1316
void changePassword(String email, String password);
17+
18+
@PreAuthorize("hasRole('ROLE_USER')")
1419
void changeEmail(String email, String newEmail);
20+
21+
@PreAuthorize("hasRole('ROLE_USER')")
1522
User findOne(long userId);
23+
24+
@PreAuthorize("hasRole('ROLE_USER')")
1625
User findByEmail(String email);
26+
27+
@PreAuthorize("hasRole('ROLE_USER')")
1728
User findByUsername(String username);
18-
List<User> findUsers();
29+
30+
@PreAuthorize("hasRole('ROLE_USER')")
1931
void remove(long id);
2032
}

src/main/java/ru/mrchebik/service/impl/CategoryServiceImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public List<Category> findAll(long userId) {
5050
return categoryRepository.findAll(userId);
5151
}
5252

53-
@Override
54-
public long findMaxLevel(long userId) {
55-
return (long) categoryRepository.findMaxLevel(userId);
56-
}
57-
5853
@Override
5954
public void remove(long id) {
6055
categoryRepository.delete(id);

src/main/java/ru/mrchebik/service/impl/SecurityServiceImpl.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
55
import org.springframework.security.core.Authentication;
66
import org.springframework.security.core.context.SecurityContextHolder;
7-
import org.springframework.security.core.userdetails.UserDetails;
87
import org.springframework.stereotype.Service;
98
import ru.mrchebik.service.SecurityService;
109

@@ -18,16 +17,6 @@ public class SecurityServiceImpl implements SecurityService {
1817
@Resource
1918
private AuthenticationManager authenticationManager;
2019

21-
@Override
22-
public String findLoggedInUsername() {
23-
Object userDetails = SecurityContextHolder.getContext().getAuthentication().getDetails();
24-
if (userDetails instanceof UserDetails) {
25-
return ((UserDetails)userDetails).getUsername();
26-
}
27-
28-
return null;
29-
}
30-
3120
@Override
3221
public void autologin(final String username, final String password) {
3322
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(username, password);

src/main/java/ru/mrchebik/service/impl/UserServiceImpl.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ru.mrchebik.service.impl;
22

3-
import org.springframework.security.access.prepost.PreAuthorize;
3+
import com.google.common.collect.Sets;
44
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
55
import org.springframework.stereotype.Repository;
66
import org.springframework.stereotype.Service;
@@ -11,8 +11,6 @@
1111
import ru.mrchebik.service.UserService;
1212

1313
import javax.annotation.Resource;
14-
import java.util.HashSet;
15-
import java.util.List;
1614

1715
/**
1816
* Created by mrchebik on 14.01.17.
@@ -31,7 +29,7 @@ public class UserServiceImpl implements UserService {
3129
@Override
3230
public User add(User user) {
3331
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
34-
user.setRoles(new HashSet<>(roleRepository.findAll()));
32+
user.setRoles(Sets.newHashSet(roleRepository.findOne(1L)));
3533
return userRepository.saveAndFlush(user);
3634
}
3735

@@ -68,12 +66,6 @@ public User findByEmail(String email) {
6866
}
6967

7068
@Override
71-
public List<User> findUsers() {
72-
return userRepository.findAll();
73-
}
74-
75-
@Override
76-
@PreAuthorize("hasRole('ROLE_ADMIN')")
7769
public void remove(long id) {
7870
userRepository.delete(id);
7971
}

0 commit comments

Comments
 (0)