Skip to content

Commit bec01aa

Browse files
committed
11_10_auth_user
1 parent 35f9ecb commit bec01aa

6 files changed

Lines changed: 21 additions & 48 deletions

File tree

‎src/main/java/ru/javawebinar/topjava/web/GlobalExceptionHandler.java‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.springframework.web.bind.annotation.ExceptionHandler;
99
import org.springframework.web.servlet.ModelAndView;
1010
import org.springframework.web.servlet.NoHandlerFoundException;
11-
import ru.javawebinar.topjava.AuthorizedUser;
1211
import ru.javawebinar.topjava.util.ValidationUtil;
1312
import ru.javawebinar.topjava.util.exception.ErrorType;
1413

@@ -44,12 +43,6 @@ private ModelAndView logAndGetExceptionView(HttpServletRequest req, Exception e,
4443
"typeMessage", messageSourceAccessor.getMessage(errorType.getErrorCode()),
4544
"status", httpStatus));
4645
mav.setStatus(httpStatus);
47-
48-
// Interceptor is not invoked, put userTo
49-
AuthorizedUser authorizedUser = SecurityUtil.safeGet();
50-
if (authorizedUser != null) {
51-
mav.addObject("userTo", authorizedUser.getUserTo());
52-
}
5346
return mav;
5447
}
5548
}

‎src/main/java/ru/javawebinar/topjava/web/interceptor/ModelInterceptor.java‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎src/main/java/ru/javawebinar/topjava/web/user/ProfileRestController.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.springframework.http.HttpStatus;
44
import org.springframework.http.MediaType;
55
import org.springframework.http.ResponseEntity;
6+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
67
import org.springframework.web.bind.annotation.*;
78
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
9+
import ru.javawebinar.topjava.AuthorizedUser;
810
import ru.javawebinar.topjava.model.User;
911
import ru.javawebinar.topjava.to.UserTo;
1012

@@ -19,14 +21,14 @@ public class ProfileRestController extends AbstractUserController {
1921
static final String REST_URL = "/rest/profile";
2022

2123
@GetMapping
22-
public User get() {
23-
return super.get(authUserId());
24+
public User get(@AuthenticationPrincipal AuthorizedUser authUser) {
25+
return super.get(authUser.getId());
2426
}
2527

2628
@DeleteMapping
2729
@ResponseStatus(HttpStatus.NO_CONTENT)
28-
public void delete() {
29-
super.delete(authUserId());
30+
public void delete(@AuthenticationPrincipal AuthorizedUser authUser) {
31+
super.delete(authUser.getId());
3032
}
3133

3234
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
@@ -40,8 +42,8 @@ public ResponseEntity<User> register(@Valid @RequestBody UserTo userTo) {
4042

4143
@PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
4244
@ResponseStatus(HttpStatus.NO_CONTENT)
43-
public void update(@Valid @RequestBody UserTo userTo) {
44-
super.update(userTo, authUserId());
45+
public void update(@Valid @RequestBody UserTo userTo, @AuthenticationPrincipal AuthorizedUser authUser) {
46+
super.update(userTo, authUser.getId());
4547
}
4648

4749
@GetMapping("/text")
@@ -50,7 +52,7 @@ public String testUTF() {
5052
}
5153

5254
@GetMapping("/with-meals")
53-
public User getWithMeals() {
54-
return super.getWithMeals(authUserId());
55+
public User getWithMeals(@AuthenticationPrincipal AuthorizedUser authUser) {
56+
return super.getWithMeals(authUser.getId());
5557
}
5658
}

‎src/main/java/ru/javawebinar/topjava/web/user/ProfileUIController.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package ru.javawebinar.topjava.web.user;
22

3+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
34
import org.springframework.stereotype.Controller;
45
import org.springframework.ui.ModelMap;
56
import org.springframework.validation.BindingResult;
67
import org.springframework.web.bind.annotation.GetMapping;
78
import org.springframework.web.bind.annotation.PostMapping;
89
import org.springframework.web.bind.annotation.RequestMapping;
910
import org.springframework.web.bind.support.SessionStatus;
11+
import ru.javawebinar.topjava.AuthorizedUser;
1012
import ru.javawebinar.topjava.to.UserTo;
11-
import ru.javawebinar.topjava.web.SecurityUtil;
1213

1314
import javax.validation.Valid;
1415

@@ -17,17 +18,18 @@
1718
public class ProfileUIController extends AbstractUserController {
1819

1920
@GetMapping
20-
public String profile() {
21+
public String profile(ModelMap model, @AuthenticationPrincipal AuthorizedUser authUser) {
22+
model.addAttribute("userTo", authUser.getUserTo());
2123
return "profile";
2224
}
2325

2426
@PostMapping
25-
public String updateProfile(@Valid UserTo userTo, BindingResult result, SessionStatus status) {
27+
public String updateProfile(@Valid UserTo userTo, BindingResult result, SessionStatus status, @AuthenticationPrincipal AuthorizedUser authUser) {
2628
if (result.hasErrors()) {
2729
return "profile";
2830
}
29-
super.update(userTo, SecurityUtil.authUserId());
30-
SecurityUtil.get().setTo(userTo);
31+
super.update(userTo, authUser.getId());
32+
authUser.setTo(userTo);
3133
status.setComplete();
3234
return "redirect:/meals";
3335
}

‎src/main/resources/spring/spring-mvc.xml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
</property>
3131
</bean>
3232
</mvc:message-converters>
33+
<mvc:argument-resolvers>
34+
<bean class="org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver "/>
35+
</mvc:argument-resolvers>
3336
</mvc:annotation-driven>
3437

3538
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
@@ -80,8 +83,6 @@
8083
</bean>
8184

8285
<mvc:interceptors>
83-
<bean class="ru.javawebinar.topjava.web.interceptor.ModelInterceptor"/>
84-
8586
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
8687
<property name="paramName" value="lang"/>
8788
</bean>

‎src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<sec:authorize access="hasRole('ADMIN')">
2020
<a class="btn btn-info mr-1" href="users"><spring:message code="user.title"/></a>
2121
</sec:authorize>
22-
<a class="btn btn-info mr-1" href="profile">${userTo.name} <spring:message code="app.profile"/></a>
22+
<a class="btn btn-info mr-1" href="profile"><sec:authentication property="principal.userTo.name"/> <spring:message code="app.profile"/></a>
2323
<button class="btn btn-primary my-1" type="submit">
2424
<span class="fa fa-sign-out"></span>
2525
</button>

0 commit comments

Comments
 (0)