Java Code Examples for org.springframework.security.crypto.password.PasswordEncoder#encode()
The following examples show how to use
org.springframework.security.crypto.password.PasswordEncoder#encode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaeldungPasswordEncoderSetup.java From tutorials with MIT License | 6 votes |
@Bean public ApplicationListener<AuthenticationSuccessEvent> authenticationSuccessListener(final PasswordEncoder encoder) { return (AuthenticationSuccessEvent event) -> { final Authentication auth = event.getAuthentication(); if (auth instanceof UsernamePasswordAuthenticationToken && auth.getCredentials() != null) { final CharSequence clearTextPass = (CharSequence) auth.getCredentials(); // 1 final String newPasswordHash = encoder.encode(clearTextPass); // 2 LOG.info("New password hash {} for user {}", newPasswordHash, auth.getName()); ((UsernamePasswordAuthenticationToken) auth).eraseCredentials(); // 3 } }; }
Example 2
Source File: Registration.java From codenjoy with GNU General Public License v3.0 | 6 votes |
public Registration(ConnectionThreadPoolFactory factory, String adminEmail, String adminPassword, PasswordEncoder passwordEncoder, ConfigProperties properties, boolean initAdminUser) { this.passwordEncoder = passwordEncoder; this.properties = properties; adminPassword = passwordEncoder.encode(Hash.md5(adminPassword)); List<String> initialScripts = new ArrayList<>(); initialScripts.add("CREATE TABLE IF NOT EXISTS users (" + "email varchar(255), " + "id varchar(255), " + "readable_name varchar(255), " + "email_approved int, " + "password varchar(255)," + "code varchar(255)," + "data varchar(255)," + "roles varchar(255));"); if (initAdminUser) { initialScripts.add(String.format("INSERT INTO users (id, email, readable_name, email_approved, password, code, data, roles)" + " select '%s', '%s', '%s', %s, '%s', '%s', '{}', '%s, %s'" + " where not exists (select 1 from users where id = '%s')", ADMIN_USER_ID, adminEmail, "admin", APPROVED, adminPassword, "000000000000", ROLE_ADMIN, ROLE_USER, ADMIN_USER_ID)); } pool = factory.create(initialScripts.toArray(new String[initialScripts.size()])); }
Example 3
Source File: UserAction.java From ExamStack with GNU General Public License v2.0 | 6 votes |
@RequestMapping(value = { "/student/change-pwd" }, method = RequestMethod.POST) public @ResponseBody Message changePassword(@RequestBody User user){ Message message = new Message(); UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); try{ String password = user.getPassword() + "{" + userInfo.getUsername() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = passwordEncoder.encode(password); user.setPassword(resultPassword); user.setUserName(userInfo.getUsername()); userService.updateUserPwd(user, null); }catch(Exception e){ e.printStackTrace(); message.setResult(e.getClass().getName()); } return message; }
Example 4
Source File: UserAction.java From ExamStack with GNU General Public License v2.0 | 6 votes |
@RequestMapping(value = { "/student/change-pwd" }, method = RequestMethod.POST) public @ResponseBody Message changePassword(@RequestBody User user){ Message message = new Message(); UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); try{ String password = user.getPassword() + "{" + userInfo.getUsername() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = passwordEncoder.encode(password); user.setPassword(resultPassword); user.setUserName(userInfo.getUsername()); userService.updateUserPwd(user, null); }catch(Exception e){ e.printStackTrace(); message.setResult(e.getClass().getName()); } return message; }
Example 5
Source File: UserAction.java From ExamStack with GNU General Public License v2.0 | 5 votes |
/** * 添加用户 * * @param user * @param groupId * 如果添加的用户为学员,必须指定groupId。如果添加的用户为教师,则groupId为任意数字 * @return */ @RequestMapping(value = { "/secure/add-user-{authority}-{groupId}" }, method = RequestMethod.POST) public @ResponseBody Message addUser(@RequestBody User user, @PathVariable String authority, @PathVariable Integer groupId) { UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); user.setCreateTime(new Date()); String password = user.getPassword() + "{" + user.getUserName().toLowerCase() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = passwordEncoder.encode(password); user.setPassword(resultPassword); user.setEnabled(true); user.setCreateBy(userInfo.getUserid()); user.setUserName(user.getUserName().toLowerCase()); Message message = new Message(); try { userService.addUser(user, authority, groupId, userInfo.getRoleMap()); } catch (Exception e) { // TODO Auto-generated catch block if(e.getMessage().contains(user.getUserName())){ message.setResult("duplicate-username"); message.setMessageInfo("重复的用户名"); } else if(e.getMessage().contains(user.getNationalId())){ message.setResult("duplicate-national-id"); message.setMessageInfo("重复的身份证"); } else if(e.getMessage().contains(user.getEmail())){ message.setResult("duplicate-email"); message.setMessageInfo("重复的邮箱"); } else if(e.getMessage().contains(user.getPhoneNum())){ message.setResult("duplicate-phone"); message.setMessageInfo("重复的电话"); } else{ message.setResult(e.getCause().getMessage()); e.printStackTrace(); } } return message; }
Example 6
Source File: UserServiceImpl.java From ExamStack with GNU General Public License v2.0 | 5 votes |
@Override public void updateUserPwd(String userName, String password, String authority) throws Exception { // TODO Auto-generated method stub User user = userMapper.getUserByName(userName); if(user.getRoles().contains(authority) && !"ROLE_ADMIN".equals(authority)) throw new Exception("教师只能更新学员的密码!"); PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); password = passwordEncoder.encode(password + "{" + userName + "}"); User tmpUser = new User(); tmpUser.setUserId(user.getUserId()); tmpUser.setPassword(password); userMapper.updateUser(tmpUser, null); }
Example 7
Source File: UserActionAdmin.java From ExamStack with GNU General Public License v2.0 | 5 votes |
/** * 添加用户 * * @param user * @param groupId * 如果添加的用户为学员,必须指定groupId。如果添加的用户为教师,则groupId为任意数字 * @return */ @RequestMapping(value = { "/admin/add-user-{authority}-{groupId}" }, method = RequestMethod.POST) public @ResponseBody Message addUser(@RequestBody User user, @PathVariable String authority, @PathVariable Integer groupId) { UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); user.setCreateTime(new Date()); String password = user.getPassword() + "{" + user.getUserName().toLowerCase() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = passwordEncoder.encode(password); user.setPassword(resultPassword); user.setEnabled(true); user.setCreateBy(userInfo.getUserid()); user.setUserName(user.getUserName().toLowerCase()); Message message = new Message(); try { userService.addUser(user, authority, groupId, userInfo.getRoleMap()); } catch (Exception e) { // TODO Auto-generated catch block if(e.getMessage().contains(user.getUserName())){ message.setResult("duplicate-username"); message.setMessageInfo("重复的用户名"); } else if(e.getMessage().contains(user.getNationalId())){ message.setResult("duplicate-national-id"); message.setMessageInfo("重复的身份证"); } else if(e.getMessage().contains(user.getEmail())){ message.setResult("duplicate-email"); message.setMessageInfo("重复的邮箱"); } else if(e.getMessage().contains(user.getPhoneNum())){ message.setResult("duplicate-phone"); message.setMessageInfo("重复的电话"); } else{ message.setResult(e.getCause().getMessage()); e.printStackTrace(); } } return message; }
Example 8
Source File: UserActionAdmin.java From ExamStack with GNU General Public License v2.0 | 5 votes |
/** * 添加用户 * * @param user * @param groupId * 如果添加的用户为学员,必须指定groupId。如果添加的用户为教师,则groupId为任意数字 * @return */ @RequestMapping(value = { "/admin/add-user-{authority}-{groupId}" }, method = RequestMethod.POST) public @ResponseBody Message addUser(@RequestBody User user, @PathVariable String authority, @PathVariable Integer groupId) { UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); user.setCreateTime(new Date()); String password = user.getPassword() + "{" + user.getUserName().toLowerCase() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = passwordEncoder.encode(password); user.setPassword(resultPassword); user.setEnabled(true); user.setCreateBy(userInfo.getUserid()); user.setUserName(user.getUserName().toLowerCase()); Message message = new Message(); try { userService.addUser(user, authority, groupId, userInfo.getRoleMap()); } catch (Exception e) { // TODO Auto-generated catch block if(e.getMessage().contains(user.getUserName())){ message.setResult("duplicate-username"); message.setMessageInfo("重复的用户名"); } else if(e.getMessage().contains(user.getNationalId())){ message.setResult("duplicate-national-id"); message.setMessageInfo("重复的身份证"); } else if(e.getMessage().contains(user.getEmail())){ message.setResult("duplicate-email"); message.setMessageInfo("重复的邮箱"); } else if(e.getMessage().contains(user.getPhoneNum())){ message.setResult("duplicate-phone"); message.setMessageInfo("重复的电话"); } else{ message.setResult(e.getCause().getMessage()); e.printStackTrace(); } } return message; }
Example 9
Source File: UserAction.java From ExamStack with GNU General Public License v2.0 | 5 votes |
@RequestMapping(value = { "/secure/update-user" }, method = RequestMethod.POST) public @ResponseBody Message updateUser(@RequestBody User user) { //UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); user.setCreateTime(new Date()); String password = user.getPassword() + "{" + user.getUserName() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = ""; if(user.getPassword() != null) resultPassword = "".equals(user.getPassword().trim()) ? "" : passwordEncoder.encode(password); user.setPassword(resultPassword); user.setEnabled(true); Message message = new Message(); try { userService.updateUser(user, null); } catch (Exception e) { // TODO Auto-generated catch block if(e.getMessage().contains(user.getUserName())){ message.setResult("duplicate-username"); message.setMessageInfo("重复的用户名"); } else if(e.getMessage().contains(user.getNationalId())){ message.setResult("duplicate-national-id"); message.setMessageInfo("重复的身份证"); } else if(e.getMessage().contains(user.getEmail())){ message.setResult("duplicate-email"); message.setMessageInfo("重复的邮箱"); } else if(e.getMessage().contains(user.getPhoneNum())){ message.setResult("duplicate-phone"); message.setMessageInfo("重复的电话"); } else{ message.setResult(e.getCause().getMessage()); e.printStackTrace(); } } return message; }
Example 10
Source File: PasswordEncoderTest.java From web-qq with Apache License 2.0 | 5 votes |
@Test public void testStandardPasswordEncoder(){ PasswordEncoder passwordEncoder = new StandardPasswordEncoder("web_qq"); String password1 = passwordEncoder.encode("12345"); String password2 = passwordEncoder.encode("mingguobin4"); System.out.println("password1:"+password1); System.out.println("password2:"+password2); System.out.println(passwordEncoder.matches("mingguobin4",password1)); }
Example 11
Source File: SecurityUserDetailsService.java From poseidon with Apache License 2.0 | 5 votes |
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Member member = memberMapper.selectByUsername(username); if (member == null) { throw new UsernameNotFoundException("user not found"); } PasswordEncoder encoder = PasswordEncoderFactories .createDelegatingPasswordEncoder(); String password = encoder.encode(member.getPassword()); return User.withUsername(username).password(password).roles("").build(); }
Example 12
Source File: UserActionAdmin.java From ExamStack with GNU General Public License v2.0 | 5 votes |
@RequestMapping(value = { "/admin/update-teacher" }, method = RequestMethod.POST) public @ResponseBody Message updateTeacher(@RequestBody User user) { UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); user.setCreateTime(new Date()); String password = user.getPassword() + "{" + user.getUserName() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = ""; if(user.getPassword() != null) resultPassword = "".equals(user.getPassword().trim()) ? "" : passwordEncoder.encode(password); user.setPassword(resultPassword); user.setEnabled(true); Message message = new Message(); try { userService.updateUser(user, null); } catch (Exception e) { // TODO Auto-generated catch block if(e.getMessage().contains(user.getUserName())){ message.setResult("duplicate-username"); message.setMessageInfo("重复的用户名"); } else if(e.getMessage().contains(user.getNationalId())){ message.setResult("duplicate-national-id"); message.setMessageInfo("重复的身份证"); } else if(e.getMessage().contains(user.getEmail())){ message.setResult("duplicate-email"); message.setMessageInfo("重复的邮箱"); } else if(e.getMessage().contains(user.getPhoneNum())){ message.setResult("duplicate-phone"); message.setMessageInfo("重复的电话"); } else{ message.setResult(e.getCause().getMessage()); e.printStackTrace(); } } return message; }
Example 13
Source File: UserAction.java From ExamStack with GNU General Public License v2.0 | 4 votes |
@RequestMapping(value = { "/student/update-user" }, method = RequestMethod.POST) public @ResponseBody Message updateUser(@RequestBody User user) { UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); String password = user.getPassword() + "{" + user.getUserName() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = ""; if(user.getPassword() != null) resultPassword = "".equals(user.getPassword().trim()) ? "" : passwordEncoder.encode(password); user.setPassword(resultPassword); user.setEnabled(true); Message message = new Message(); try { userService.updateUser(user, null); userInfo.setTrueName(user.getTrueName()); userInfo.setEmail(user.getEmail()); userInfo.setNationalId(user.getNationalId()); userInfo.setPhoneNum(user.getPhoneNum()); } catch (Exception e) { // TODO Auto-generated catch block if(e.getMessage().contains(user.getUserName())){ message.setResult("duplicate-username"); message.setMessageInfo("重复的用户名"); } else if(e.getMessage().contains(user.getNationalId())){ message.setResult("duplicate-national-id"); message.setMessageInfo("重复的身份证"); } else if(e.getMessage().contains(user.getEmail())){ message.setResult("duplicate-email"); message.setMessageInfo("重复的邮箱"); } else if(e.getMessage().contains(user.getPhoneNum())){ message.setResult("duplicate-phone"); message.setMessageInfo("重复的电话"); } else{ message.setResult(e.getCause().getMessage()); e.printStackTrace(); } } return message; }
Example 14
Source File: UserAction.java From ExamStack with GNU General Public License v2.0 | 4 votes |
@RequestMapping(value = { "/student/update-user" }, method = RequestMethod.POST) public @ResponseBody Message updateUser(@RequestBody User user) { UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); String password = user.getPassword() + "{" + user.getUserName() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = ""; if(user.getPassword() != null) resultPassword = "".equals(user.getPassword().trim()) ? "" : passwordEncoder.encode(password); user.setPassword(resultPassword); user.setEnabled(true); Message message = new Message(); try { userService.updateUser(user, null); userInfo.setTrueName(user.getTrueName()); userInfo.setEmail(user.getEmail()); userInfo.setNationalId(user.getNationalId()); userInfo.setPhoneNum(user.getPhoneNum()); } catch (Exception e) { // TODO Auto-generated catch block if(e.getMessage().contains(user.getUserName())){ message.setResult("duplicate-username"); message.setMessageInfo("重复的用户名"); } else if(e.getMessage().contains(user.getNationalId())){ message.setResult("duplicate-national-id"); message.setMessageInfo("重复的身份证"); } else if(e.getMessage().contains(user.getEmail())){ message.setResult("duplicate-email"); message.setMessageInfo("重复的邮箱"); } else if(e.getMessage().contains(user.getPhoneNum())){ message.setResult("duplicate-phone"); message.setMessageInfo("重复的电话"); } else{ message.setResult(e.getCause().getMessage()); e.printStackTrace(); } } return message; }
Example 15
Source File: RegistrationForm.java From spring-in-action-5-samples with Apache License 2.0 | 4 votes |
public User toUser(PasswordEncoder passwordEncoder) { return new User( username, passwordEncoder.encode(password), fullname, street, city, state, zip, phone); }
Example 16
Source File: PdfEncryptor.java From website with GNU Affero General Public License v3.0 | 4 votes |
public static String generatedOwnerPassword() { PasswordEncoder passwordEncoder = new StandardPasswordEncoder(); String password = "" + DateTime.now().getMillis(); return passwordEncoder.encode(password); }
Example 17
Source File: RegistrationForm.java From spring-in-action-5-samples with Apache License 2.0 | 4 votes |
public User toUser(PasswordEncoder passwordEncoder) { return new User( username, passwordEncoder.encode(password), fullname, street, city, state, zip, phone); }
Example 18
Source File: RegistrationForm.java From spring-in-action-5-samples with Apache License 2.0 | 4 votes |
public User toUser(PasswordEncoder passwordEncoder) { return new User( username, passwordEncoder.encode(password), fullname, street, city, state, zip, phone); }
Example 19
Source File: SystemActionAdmin.java From ExamStack with GNU General Public License v2.0 | 4 votes |
/** * 添加用户 * * @param user * @param groupId * 如果添加的用户为学员,必须指定groupId。如果添加的用户为教师,则groupId为任意数字 * @return */ @RequestMapping(value = { "/admin/add-admin" }, method = RequestMethod.POST) public @ResponseBody Message addUser(@RequestBody User user) { UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); Message message = new Message(); if(!userInfo.getAuthorities().toString().contains("ROLE_ADMIN")){ message.setResult("权限错误!"); message.setMessageInfo("只有管理员才可以添加管理员!"); return message; } user.setCreateTime(new Date()); String password = user.getPassword() + "{" + user.getUserName().toLowerCase() + "}"; PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1(); String resultPassword = passwordEncoder.encode(password); user.setPassword(resultPassword); user.setEnabled(true); user.setCreateBy(userInfo.getUserid()); user.setUserName(user.getUserName().toLowerCase()); try { userService.addUser(user, "ROLE_ADMIN", -1, userInfo.getRoleMap()); } catch (Exception e) { // TODO Auto-generated catch block if(e.getMessage().contains(user.getUserName())){ message.setResult("duplicate-username"); message.setMessageInfo("重复的用户名"); } else if(e.getMessage().contains(user.getNationalId())){ message.setResult("duplicate-national-id"); message.setMessageInfo("重复的身份证"); } else if(e.getMessage().contains(user.getEmail())){ message.setResult("duplicate-email"); message.setMessageInfo("重复的邮箱"); } else if(e.getMessage().contains(user.getPhoneNum())){ message.setResult("duplicate-phone"); message.setMessageInfo("重复的电话"); } else{ message.setResult(e.getCause().getMessage()); e.printStackTrace(); } } return message; }
Example 20
Source File: RegistrationForm.java From spring-in-action-5-samples with Apache License 2.0 | 4 votes |
public User toUser(PasswordEncoder passwordEncoder) { return new User( username, passwordEncoder.encode(password), fullname, street, city, state, zip, phone, email); }