@Bean CommandLineRunner init( AccountService accountService ) { return (evt) -> Arrays.asList( "user,admin,john,robert,ana".split(",")).forEach( username -> { Account acct = new Account(); acct.setUsername(username); if ( username.equals("user")) acct.setPassword("password"); else acct.setPassword(passwordEncoder().encode("password")); acct.setFirstName(username); acct.setLastName("LastName"); acct.grantAuthority("ROLE_USER"); if ( username.equals("admin") ) acct.grantAuthority("ROLE_ADMIN"); try { accountService.register(acct); } catch (AccountException e) { e.printStackTrace(); } } ); }
public Account register(Account account) throws AccountException { if ( accountRepo.countByUsername( account.getUsername() ) == 0 ) { account.setPassword(passwordEncoder.encode(account.getPassword())); return accountRepo.save( account ); } else { throw new AccountException(String.format("Username[%s] already taken.", account.getUsername())); } }
@PostMapping(path = "/api/register", produces = "application/json") public ResponseEntity<?> register(@RequestBody Account account) { try { account.grantAuthority("ROLE_USER"); return new ResponseEntity<Object>( accountService.register( account ), HttpStatus.OK); } catch (AccountException e) { e.printStackTrace(); return new ResponseEntity<RestError>(new RestError(e.getMessage()),HttpStatus.BAD_REQUEST ); } }
/** * @tests javax.security.auth.login.AccountException#AccountException( * java.lang.String) */ public final void testCtor2() { assertNull(new AccountException(null).getMessage()); String message = ""; assertSame(message, new AccountException(message).getMessage()); message = "message"; assertSame(message, new AccountException(message).getMessage()); }
/** * 登陆验证失败跳转 * * @param userName * @param model * @return */ @RequestMapping(value = "/login", method = RequestMethod.POST) public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String userName, HttpServletRequest req, Model model) { String exceptionClassName = (String) req.getAttribute("shiroLoginFailure"); String error = null; if (UnknownAccountException.class.getName().equals(exceptionClassName)) { error = "您还没有注册,请注册使用!"; } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) { error = "用户名/密码不正确!"; } else if (CaptchaInvalidException.class.getName().equals(exceptionClassName)) { error = "图形验证码已经失效,请重新刷新页面!"; } else if (CaptchaException.class.getName().equals(exceptionClassName)) { error = "图形验证码错误!"; } else if (AccountException.class.getName().equals(exceptionClassName)) { error = "用户名/密码输入出错!"; } else if (exceptionClassName != null) { error = "登录失败,请重试!"; } if (StringUtils.isEmpty(exceptionClassName)) { String code = request.getParameter("captcha"); String phone = request.getParameter("username"); String pwd = request.getParameter("password"); if (StringUtils.equalsIgnoreCase(code, (String) session.getAttribute(ValidateCodeServlet.VALIDATE_CODE))) { updateShiroUser(phone, pwd); return "redirect:/user/setting"; } else { error = "图形验证码错误!"; } } model.addAttribute("errMsg", error); // ajax未登入情况 if (InvokeTypeTools.isAjax(req)) { response.setContentType("application/json"); PrintWriter writer = null; try { writer = response.getWriter(); } catch (IOException e1) { e1.printStackTrace(); } writer.print(JsonResultUtils.createJsonResult(ResultCode.ERROR, "", error)); writer.flush(); writer.close(); } return "account/login"; }
public boolean authenticate(String username, String password) throws LoginException { if (ldapServerUrls == null || ldapServerUrls.length == 0) { throw new AccountException("Unable to find ldap servers"); } if (username == null || password == null || username.trim().length() == 0 || password.trim().length() == 0) { throw new FailedLoginException("Username or password is empty"); } int retryCount = 0; int currentLdapUrlIndex = lastLdapUrlIndex; do { retryCount++; try { Hashtable<Object, Object> env = new Hashtable<Object, Object>(); env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY_CLASS); env.put(Context.PROVIDER_URL, ldapServerUrls[currentLdapUrlIndex]); env.put(Context.SECURITY_PRINCIPAL, username + "@" + domainName); env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = new InitialDirContext(env); ctx.close(); lastLdapUrlIndex = currentLdapUrlIndex; return true; } catch (CommunicationException exp) { // TODO you can replace with log4j or slf4j API exp.printStackTrace(); // if the exception of type communication we can assume the AD // is not reachable hence retry can be attempted with next // available AD if (retryCount < ldapServerUrls.length) { currentLdapUrlIndex++; if (currentLdapUrlIndex == ldapServerUrls.length) { currentLdapUrlIndex = 0; } continue; } return false; } catch (Throwable throwable) { throwable.printStackTrace(); return false; } } while (true); }
@Override protected Object[] getData() { return new Object[] {new AccountException("message")}; }
/** * @tests javax.security.auth.login.AccountException#AccountException() */ public final void testCtor1() { assertNull(new AccountException().getMessage()); }