Java tutorial
/* * Copyright 2014 Azige. * * 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 io.github.azige.bbs.web.controller; import java.io.IOException; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import io.github.azige.bbs.data.ProfileRepository; import io.github.azige.bbs.entity.Account; import io.github.azige.bbs.service.ServiceException; import io.github.azige.bbs.entity.Profile; import io.github.azige.bbs.service.AccountService; import io.github.azige.bbs.web.ErrorResult; import io.github.azige.bbs.web.RegisterForm; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * * @author Azige */ @Controller public class AccountController { @Autowired private ProfileRepository profileRepository; @Autowired private AccountService accountService; @Autowired private MessageSource messageSource; @RequestMapping(value = "/profile/{id}") public String profileView(@PathVariable Long id, Model model) { Profile profile = profileRepository.findOne(id); if (profile == null) { model.addAttribute("error", "err.profileNotExist"); return "error"; } else { model.addAttribute("profile", profile); return "profile"; } } @RequestMapping(value = "/profile/current.json", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Profile profileCurrent() { return (Profile) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); } // @RequestMapping(value = "/login.ajax", method = RequestMethod.POST, // consumes = "application/json", produces = "application/json; charset=UTF-8") public ResponseEntity<?> loginAjax(@RequestBody Account account, Model model) throws IOException { try { Authentication authentication = accountService.authenticate( new UsernamePasswordAuthenticationToken(account.getAccountName(), account.getPassword())); SecurityContextHolder.getContext().setAuthentication(authentication); Profile loginProfile = (Profile) authentication.getPrincipal(); return new ResponseEntity<>(loginProfile, HttpStatus.OK); } catch (AuthenticationException ex) { return new ResponseEntity<>( new ErrorResult( messageSource.getMessage("account.login.fail", null, LocaleContextHolder.getLocale())), HttpStatus.UNAUTHORIZED); } } @RequestMapping(value = "/register", method = RequestMethod.GET) public String registerForm(RegisterForm registerForm, Model model) { model.addAttribute("command", registerForm); return "register"; } @RequestMapping(value = "/register", method = RequestMethod.POST) public String registerSubmit(RegisterForm form, Model model, HttpServletRequest request) { try { Profile profile = accountService.register(form.getName(), form.getPassword(), form.getNickName(), form.getDescription()); request.getSession().setAttribute("loginProfile", profile); } catch (ServiceException ex) { model.addAttribute("registerForm", form); model.addAttribute("error", ex.getMessage()); return "register"; } return "redirect:/"; } }