Java tutorial
/* * Copyright (c) 2016 Prasenjit Purohit * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.prasenjit.auth.controller; import net.prasenjit.auth.domain.User; import net.prasenjit.auth.model.ChangePasswordRequest; import net.prasenjit.auth.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; /** * Created by PRASENJIT-NET on 4/20/2016. * * @author PRASEN * @version $Id: $Id */ @RestController public class MeController { @Autowired private UserService userService; /** * <p>me.</p> * * @return a {@link net.prasenjit.auth.domain.User} object. */ @RequestMapping(value = "/api/me", method = RequestMethod.GET) public User me() { return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); } /** * <p>changePassword.</p> * * @param request a {@link net.prasenjit.auth.model.ChangePasswordRequest} object. */ @RequestMapping(value = "/api/me", method = RequestMethod.PUT, params = "operation=changePassword") @ResponseStatus(HttpStatus.ACCEPTED) public void changePassword(@Validated @RequestBody ChangePasswordRequest request) { boolean status = userService.changePassword(request.getCurrentPassword(), request.getNewPassword()); if (!status) { throw new RuntimeException("Failed to change password"); } } }