Skip to content

Commit

Permalink
11_10_auth_user
Browse files Browse the repository at this point in the history
  • Loading branch information
GlyzinAI committed Sep 8, 2019
1 parent 29f19f7 commit d97657c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import ru.javawebinar.topjava.AuthorizedUser;
import ru.javawebinar.topjava.util.ValidationUtil;
import ru.javawebinar.topjava.util.exception.ErrorType;

Expand Down Expand Up @@ -37,12 +36,6 @@ private ModelAndView logAndGetExceptionView(HttpServletRequest req, Exception e,
mav.addObject("typeMessage", messageUtil.getMessage(errorType.getErrorCode()));
mav.addObject("exception", rootCause);
mav.addObject("message", ValidationUtil.getMessage(rootCause));

// Interceptor is not invoked, put userTo
AuthorizedUser authorizedUser = SecurityUtil.safeGet();
if (authorizedUser != null) {
mav.addObject("userTo", authorizedUser.getUserTo());
}
return mav;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import ru.javawebinar.topjava.AuthorizedUser;
import ru.javawebinar.topjava.model.User;
import ru.javawebinar.topjava.to.UserTo;

import javax.validation.Valid;
import java.net.URI;

import static ru.javawebinar.topjava.web.SecurityUtil.authUserId;

@RestController
@RequestMapping(ProfileRestController.REST_URL)
public class ProfileRestController extends AbstractUserController {
static final String REST_URL = "/rest/profile";

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public User get() {
return super.get(authUserId());
public User get(@AuthenticationPrincipal AuthorizedUser authUser) {
return super.get(authUser.getId());
}

@DeleteMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete() {
super.delete(authUserId());
public void delete(@AuthenticationPrincipal AuthorizedUser authUser) {
super.delete(authUser.getId());
}

@PostMapping(value = "/register", consumes = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -42,8 +42,8 @@ public ResponseEntity<User> register(@Valid @RequestBody UserTo userTo) {

@PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void update(@Valid @RequestBody UserTo userTo) {
super.update(userTo, authUserId());
public void update(@Valid @RequestBody UserTo userTo, @AuthenticationPrincipal AuthorizedUser authUser) {
super.update(userTo, authUser.getId());
}

@GetMapping(value = "/text")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package ru.javawebinar.topjava.web.user;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.support.SessionStatus;
import ru.javawebinar.topjava.AuthorizedUser;
import ru.javawebinar.topjava.to.UserTo;
import ru.javawebinar.topjava.web.SecurityUtil;

import javax.validation.Valid;

Expand All @@ -17,17 +18,18 @@
public class ProfileUIController extends AbstractUserController {

@GetMapping
public String profile() {
public String profile(ModelMap model, @AuthenticationPrincipal AuthorizedUser authUser) {
model.addAttribute("userTo", authUser.getUserTo());
return "profile";
}

@PostMapping
public String updateProfile(@Valid UserTo userTo, BindingResult result, SessionStatus status) {
public String updateProfile(@Valid UserTo userTo, BindingResult result, SessionStatus status, @AuthenticationPrincipal AuthorizedUser authUser) {
if (result.hasErrors()) {
return "profile";
}
super.update(userTo, SecurityUtil.authUserId());
SecurityUtil.get().update(userTo);
super.update(userTo, authUser.getId());
authUser.update(userTo);
status.setComplete();
return "redirect:/meals";
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/spring/spring-mvc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<!-- </property>-->
</bean>
</mvc:message-converters>
<mvc:argument-resolvers>
<bean class="org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver "/>
</mvc:argument-resolvers>
</mvc:annotation-driven>

<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
Expand Down Expand Up @@ -76,8 +79,6 @@
</bean>

<mvc:interceptors>
<bean class="ru.javawebinar.topjava.web.interceptor.ModelInterceptor"/>

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<sec:authorize access="hasRole('ROLE_ADMIN')">
<a class="btn btn-info mr-1" href="users"><spring:message code="user.title"/></a>
</sec:authorize>
<a class="btn btn-info mr-1" href="profile">${userTo.name} <spring:message code="app.profile"/></a>
<a class="btn btn-info mr-1" href="profile"><sec:authentication property="principal.userTo.name"/> <spring:message code="app.profile"/></a>
<button class="btn btn-primary my-1" type="submit">
<span class="fa fa-sign-out"></span>
</button>
Expand Down

0 comments on commit d97657c

Please sign in to comment.