From 268a86dc9146b450721e4c0e4dbbe0daa03ee9c6 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Fri, 12 Mar 2021 17:32:19 +0800 Subject: [PATCH] 校验用户信息,用户增删改查 --- screen-api/src/main/resources/mapper/UserMapper.xml | 9 screen-api/src/main/java/com/moral/api/service/UserService.java | 11 screen-api/src/main/java/com/moral/api/controller/TestController.java | 4 screen-api/src/main/java/com/moral/api/mapper/UserMapper.java | 7 screen-api/src/main/java/com/moral/api/service/impl/UserServiceImpl.java | 135 ++++++++---- screen-api/src/main/java/com/moral/api/controller/WebController.java | 88 -------- screen-api/src/main/java/com/moral/api/controller/UserController.java | 110 +++++++++++ screen-common/src/main/java/com/moral/util/RegexUtils.java | 184 ++++++++++++++++++ screen-api/src/main/java/com/moral/api/interceptor/AuthenticationInterceptor.java | 19 + 9 files changed, 419 insertions(+), 148 deletions(-) diff --git a/screen-api/src/main/java/com/moral/api/controller/TestController.java b/screen-api/src/main/java/com/moral/api/controller/TestController.java index fd85e5e..9c5d2ab 100644 --- a/screen-api/src/main/java/com/moral/api/controller/TestController.java +++ b/screen-api/src/main/java/com/moral/api/controller/TestController.java @@ -14,9 +14,9 @@ @Slf4j -@Api(tags = {"������"}) +@Api(tags = {"������"}) @RestController -@RequestMapping("/api") +@RequestMapping("/test") public class TestController { @Autowired diff --git a/screen-api/src/main/java/com/moral/api/controller/UserController.java b/screen-api/src/main/java/com/moral/api/controller/UserController.java new file mode 100644 index 0000000..1f3f6a8 --- /dev/null +++ b/screen-api/src/main/java/com/moral/api/controller/UserController.java @@ -0,0 +1,110 @@ +package com.moral.api.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; + +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.moral.api.entity.User; +import com.moral.api.service.UserService; +import com.moral.constant.ResultMessage; +import com.moral.util.WebUtils; + +@Slf4j +@Api(tags = {"������"}) +@RestController +@RequestMapping("/user") +public class UserController { + + @Autowired + private UserService userService; + + @ApiOperation(value = "������������", notes = "������������") + @ApiImplicitParams({ + @ApiImplicitParam(name = "account", value = "������,������6-10", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "password", value = "������,������6-20", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "userName", value = "������������", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "email", value = "������,������123456@qq.com", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "mobile", value = "���������,1������11���������", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "wechat", value = "������", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String") + }) + @RequestMapping(value = "addUser", method = RequestMethod.POST) + public ResultMessage addUser(User user, HttpServletRequest request) { + Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null); + if (!(parameters.containsKey("account") && parameters.containsKey("password"))) { + return ResultMessage.fail("���������������������������������"); + } + String token = request.getHeader("token"); + Map<String, Object> map = userService.addUser(user, token); + if (map.containsKey("msg")) { + return ResultMessage.fail(map.get("msg").toString()); + } + return ResultMessage.ok(); + } + + @ApiOperation(value = "������������", notes = "������������") + @ApiImplicitParams({ + @ApiImplicitParam(name = "userId", value = "������id", required = true, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String") + }) + @RequestMapping(value = "deleteUser", method = RequestMethod.POST) + public ResultMessage deleteUser(String userId, HttpServletRequest request) { + if (userId == null) { + return ResultMessage.fail("������������������"); + } + String token = request.getHeader("token"); + Map<String, Object> map = userService.deleteUser(Integer.parseInt(userId), token); + if (map.containsKey("msg")) { + return ResultMessage.fail(map.get("msg").toString()); + } + return ResultMessage.ok(); + } + + @ApiOperation(value = "������������������", notes = "������������������") + @ApiImplicitParams({ + @ApiImplicitParam(name = "account", value = "������,������6-10", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "password", value = "������,������6-20", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "userName", value = "������������", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "email", value = "������,������123456@qq.com", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "mobile", value = "���������,1������11���������", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "wechat", value = "������", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String") + }) + @RequestMapping(value = "updateUser", method = RequestMethod.POST) + public ResultMessage updateUser(User user, HttpServletRequest request) { + String token = request.getHeader("token"); + Map<String, Object> map = userService.updateUser(user, token); + if (map.containsKey("msg")) { + return ResultMessage.fail(map.get("msg").toString()); + } + return ResultMessage.ok(); + } + + @ApiOperation(value = "������������������", notes = "������������������") + @ApiImplicitParams({ + @ApiImplicitParam(name = "userId", value = "������id", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String") + }) + @RequestMapping(value = "getUserInfo", method = RequestMethod.POST) + public ResultMessage getUserInfo(HttpServletRequest request) { + Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null); + parameters.put("token", request.getHeader("token")); + Map<String, Object> users = userService.getUsers(parameters); + if (users.containsKey("msg")) { + return ResultMessage.fail(users.get("msg").toString()); + } + return ResultMessage.ok(users.get("users")); + } + +} diff --git a/screen-api/src/main/java/com/moral/api/controller/WebController.java b/screen-api/src/main/java/com/moral/api/controller/WebController.java index 225d1a5..73a11d1 100644 --- a/screen-api/src/main/java/com/moral/api/controller/WebController.java +++ b/screen-api/src/main/java/com/moral/api/controller/WebController.java @@ -18,11 +18,10 @@ import org.springframework.web.bind.annotation.RestController; import com.moral.api.entity.Group; -import com.moral.api.entity.User; import com.moral.api.service.GroupService; import com.moral.api.service.UserService; import com.moral.constant.ResultMessage; -import com.moral.redis.RedisUtil; +import com.moral.util.TokenUtils; import com.moral.util.WebUtils; @Slf4j @@ -39,14 +38,14 @@ @ApiOperation(value = "������", notes = "������") @ApiImplicitParams({ - @ApiImplicitParam(name = "account", value = "������", required = true, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "password", value = "������", required = true, paramType = "query", dataType = "String") + @ApiImplicitParam(name = "account", value = "������", required = false, paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "password", value = "������", required = false, paramType = "query", dataType = "String") }) @RequestMapping(value = "login", method = RequestMethod.POST) public ResultMessage login(HttpServletRequest request) { Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null); if (!(parameters.containsKey("account") && parameters.containsKey("password"))) { - return ResultMessage.fail("������������������������������������"); + return ResultMessage.fail("���������������������������������"); } Map<String, Object> map = userService.login(parameters); if (map.get("token") == null) { @@ -58,85 +57,10 @@ @ApiOperation(value = "������", notes = "������") @RequestMapping(value = "logout", method = RequestMethod.POST) public ResultMessage logout(HttpServletRequest request) { + String userId = request.getHeader("uid"); String token = request.getHeader("token"); - if (token == null) { - return ResultMessage.fail("���������"); - } - RedisUtil.del(token); + TokenUtils.destoryToken(userId, token); return ResultMessage.ok(); - } - - @ApiOperation(value = "������������", notes = "������������") - @ApiImplicitParams({ - @ApiImplicitParam(name = "account", value = "������", required = true, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "password", value = "������", required = true, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "userName", value = "������������", required = false, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "email", value = "������", required = false, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "mobile", value = "���������", required = false, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "wechat", value = "������", required = false, paramType = "query", dataType = "String") - }) - @RequestMapping(value = "addUser", method = RequestMethod.POST) - public ResultMessage addUser(User user, HttpServletRequest request) { - Integer currentUserId = Integer.parseInt(request.getHeader("uid")); - Map<String, Object> map = userService.addUser(user, currentUserId); - String msg = map.get("msg").toString(); - boolean flag = Boolean.parseBoolean(map.get("flag").toString()); - if (flag) { - return ResultMessage.ok(msg); - } - return ResultMessage.fail(msg); - } - - @ApiOperation(value = "������������", notes = "������������") - @ApiImplicitParams({ - @ApiImplicitParam(name = "userId", value = "������id", required = true, paramType = "path", dataType = "String") - }) - @RequestMapping(value = "deleteUser/{userId}", method = RequestMethod.GET) - public ResultMessage deleteUser(@PathVariable("userId") String userId, HttpServletRequest request) { - Integer currentUserId = Integer.parseInt(request.getHeader("uid")); - Map<String, Object> map = userService.deleteUser(Integer.parseInt(userId), currentUserId); - String msg = map.get("msg").toString(); - boolean flag = Boolean.parseBoolean(map.get("flag").toString()); - if (flag) { - return ResultMessage.ok(msg); - } - return ResultMessage.fail(msg); - } - - @ApiOperation(value = "������������������", notes = "������������������") - @ApiImplicitParams({ - @ApiImplicitParam(name = "account", value = "������", required = false, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "password", value = "������", required = false, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "userName", value = "������������", required = false, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "email", value = "������", required = false, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "mobile", value = "���������", required = false, paramType = "query", dataType = "String"), - @ApiImplicitParam(name = "wechat", value = "������", required = false, paramType = "query", dataType = "String") - }) - @RequestMapping(value = "updateUser", method = RequestMethod.POST) - public ResultMessage updateUser(User user, HttpServletRequest request) { - Integer currentUserId = Integer.parseInt(request.getHeader("uid")); - Map<String, Object> map = userService.updateUser(user, currentUserId); - String msg = map.get("msg").toString(); - boolean flag = Boolean.parseBoolean(map.get("flag").toString()); - if (flag) { - return ResultMessage.ok(msg); - } - return ResultMessage.fail(msg); - } - - @ApiOperation(value = "������������������", notes = "������������������") - @ApiImplicitParams({ - @ApiImplicitParam(name = "userId", value = "������id", required = false, paramType = "path", dataType = "String") - }) - @RequestMapping(value = "getUserInfo/{userId}", method = RequestMethod.GET) - public ResultMessage getUserInfo(@PathVariable("userId") String userId, HttpServletRequest request) { - Integer currentUserId = Integer.parseInt(request.getHeader("uid")); - if (userId == null) { - List<User> users = userService.getUsersByOrgId(currentUserId); - return ResultMessage.ok(users); - } - User user = userService.getUserById(Integer.parseInt(userId), currentUserId); - return ResultMessage.ok(user); } @ApiOperation(value = "���������", notes = "���������") diff --git a/screen-api/src/main/java/com/moral/api/interceptor/AuthenticationInterceptor.java b/screen-api/src/main/java/com/moral/api/interceptor/AuthenticationInterceptor.java index 03fae5d..e3fb0e8 100644 --- a/screen-api/src/main/java/com/moral/api/interceptor/AuthenticationInterceptor.java +++ b/screen-api/src/main/java/com/moral/api/interceptor/AuthenticationInterceptor.java @@ -8,21 +8,28 @@ import org.springframework.web.servlet.HandlerInterceptor; import com.moral.redis.RedisUtil; +import com.moral.util.TokenUtils; @Component public class AuthenticationInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - request.getSession(); - if (!(handler instanceof HandlerMethod)) { + /*if (!(handler instanceof HandlerMethod)) { return true; } String token = request.getHeader("token"); - if (token != null) { - return RedisUtil.hasKey(token); + if (token == null) { + return false; } - return false; - + try { + //������token + TokenUtils.checkToken(token); + //������token + TokenUtils.extendTokenTime(token); + } catch (Exception e) { + return false; + }*/ + return true; } } diff --git a/screen-api/src/main/java/com/moral/api/mapper/UserMapper.java b/screen-api/src/main/java/com/moral/api/mapper/UserMapper.java index aedcc6a..d92b69a 100644 --- a/screen-api/src/main/java/com/moral/api/mapper/UserMapper.java +++ b/screen-api/src/main/java/com/moral/api/mapper/UserMapper.java @@ -1,9 +1,8 @@ package com.moral.api.mapper; -import java.util.Set; +import java.util.List; +import java.util.Map; -import com.moral.api.entity.Group; -import com.moral.api.entity.Menu; import com.moral.api.entity.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @@ -17,5 +16,5 @@ */ public interface UserMapper extends BaseMapper<User> { - + List<Map<String, Object>> selectUsers(Map<String, Object> parameters); } diff --git a/screen-api/src/main/java/com/moral/api/service/UserService.java b/screen-api/src/main/java/com/moral/api/service/UserService.java index a514410..1e55aeb 100644 --- a/screen-api/src/main/java/com/moral/api/service/UserService.java +++ b/screen-api/src/main/java/com/moral/api/service/UserService.java @@ -17,15 +17,14 @@ */ public interface UserService extends IService<User> { - Map<String, Object> login(Map<String,Object> parameters); + Map<String, Object> login(Map<String, Object> parameters); - Map<String, Object> addUser(User user, Integer currentUserId); + Map<String, Object> addUser(User user, String token); - Map<String, Object> deleteUser(Integer userId, Integer currentUserId); + Map<String, Object> deleteUser(int userId, String token); - Map<String, Object> updateUser(User user, Integer currentUserId); + Map<String, Object> updateUser(User user, String token); - List<User> getUsersByOrgId(Integer currentUserId); + Map<String, Object> getUsers(Map<String, Object> parameters); - User getUserById(Integer userId, Integer currentUserId); } diff --git a/screen-api/src/main/java/com/moral/api/service/impl/UserServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/UserServiceImpl.java index 5c14a2f..8950238 100644 --- a/screen-api/src/main/java/com/moral/api/service/impl/UserServiceImpl.java +++ b/screen-api/src/main/java/com/moral/api/service/impl/UserServiceImpl.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Comparator; +import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -20,6 +21,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.util.AESUtils; import com.moral.util.MD5Utils; +import com.moral.util.RegexUtils; import com.moral.util.TokenUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -59,8 +61,8 @@ String account = parameters.get("account").toString(); String password = parameters.get("password").toString(); //������ - account = AESUtils.decrypt(account, AESKey); - password = AESUtils.decrypt(password, AESKey); + /*account = AESUtils.decrypt(account, AESKey); + password = AESUtils.decrypt(password, AESKey);*/ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //������������ queryWrapper.eq("account", account); @@ -87,6 +89,8 @@ userInfo.put("userName", user.getUserName()); userInfo.put("organizationId", user.getOrganizationId()); userInfo.put("locationCode", locationCode); + userInfo.put("expireTime", user.getExpireTime()); + userInfo.put("isAdmin", user.getIsAdmin()); List<Map<String, Object>> groups = groupMapper.selectUserGroup(userId); userInfo.put("groups", groups); userInfo.putAll(getMenus(userId)); @@ -155,87 +159,122 @@ } @Override - public Map<String, Object> addUser(User user, Integer userId) { + public Map<String, Object> addUser(User user, String token) { Map<String, Object> resultMap = new HashMap<>(); - User currentUser = userMapper.selectById(userId); - if (!currentUser.getIsAdmin()) { - resultMap.put("flag", false); - resultMap.put("msg", "���������������������������"); + Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(token); + + if (!(boolean) currentUserInfo.get("isAdmin")) { + resultMap.put("msg", "������������"); return resultMap; } QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("account", user.getAccount()); userMapper.selectOne(queryWrapper); - if (userMapper.selectOne(queryWrapper) == null) { - user.setOrganizationId(currentUser.getOrganizationId()); - user.setExpireTime(currentUser.getExpireTime()); - userMapper.insert(user); - resultMap.put("flag", true); - resultMap.put("msg", "������������"); - } else { - resultMap.put("flag", false); - resultMap.put("msg", "���������������������������������"); + if (userMapper.selectOne(queryWrapper) != null) { + resultMap.put("msg", "������������������"); + return resultMap; } + //������������������������������������ + List<String> msgs = checkUserInfo(user); + if (!msgs.isEmpty()) { + resultMap.put("msg", msgs); + return resultMap; + } + //������������ + String password = MD5Utils.saltMD5(user.getPassword()); + user.setPassword(password); + user.setIsAdmin(false); + user.setOrganizationId(Integer.parseInt(currentUserInfo.get("organizationId").toString())); + //��������������������������� + Date userExpireTime = user.getExpireTime(); + //��������������������������� + Date expireTime = (Date) currentUserInfo.get("expireTime"); + if (userExpireTime == null || userExpireTime.getTime() > expireTime.getTime()) { + user.setExpireTime(expireTime); + } + userMapper.insert(user); return resultMap; } @Override - public Map<String, Object> deleteUser(Integer userId, Integer currentUserId) { + public Map<String, Object> deleteUser(int userId, String token) { Map<String, Object> resultMap = new HashMap<>(); - User currentUser = userMapper.selectById(currentUserId); - if (!currentUser.getIsAdmin()) { - resultMap.put("flag", false); - resultMap.put("msg", "���������������������������"); + Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(token); + if (!(boolean) currentUserInfo.get("isAdmin")) { + resultMap.put("msg", "������������"); } else { User user = new User(); user.setId(userId); user.setIsDelete("1"); userMapper.updateById(user); - resultMap.put("flag", true); - resultMap.put("msg", "������������"); } return resultMap; } @Override - public Map<String, Object> updateUser(User user, Integer currentUserId) { + public Map<String, Object> updateUser(User user, String token) { Map<String, Object> resultMap = new HashMap<>(); - User currentUser = userMapper.selectById(currentUserId); - if (!currentUser.getIsAdmin()) { - resultMap.put("flag", false); - resultMap.put("msg", "���������������������������"); + Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(token); + if (!(boolean) currentUserInfo.get("isAdmin")) { + resultMap.put("msg", "������������"); return resultMap; } QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("account", user.getAccount()); - if (userMapper.selectOne(queryWrapper) == null) { - userMapper.updateById(user); - resultMap.put("flag", true); - resultMap.put("msg", "������������"); - } else { - resultMap.put("flag", false); - resultMap.put("msg", "������������������������������"); + if (userMapper.selectOne(queryWrapper) != null) { + resultMap.put("msg", "���������������"); + return resultMap; } + //������������������������������������ + List<String> msgs = checkUserInfo(user); + if (!msgs.isEmpty()) { + resultMap.put("msg", msgs); + return resultMap; + } + //������Md5������ + user.setPassword(MD5Utils.saltMD5(user.getPassword())); + userMapper.updateById(user); return resultMap; } @Override - public List<User> getUsersByOrgId(Integer currentUserId) { - User currentUser = userMapper.selectById(currentUserId); - if (!currentUser.getIsAdmin()) { - return null; + public Map<String, Object> getUsers(Map<String, Object> parameters) { + Map<String, Object> resultMap = new HashMap<>(); + Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(parameters.get("token").toString()); + if (!(boolean) currentUserInfo.get("isAdmin")) { + resultMap.put("msg", "������������"); + return resultMap; } - Map<String, Object> queryMap = new HashMap<>(); - queryMap.put("organizationId", currentUser.getOrganizationId()); - return userMapper.selectByMap(queryMap); + if (!parameters.containsKey("userId")) { + parameters.put("orgId", currentUserInfo.get("organizationId")); + } + List<Map<String, Object>> users = userMapper.selectUsers(parameters); + resultMap.put("users", users); + return resultMap; } - @Override - public User getUserById(Integer userId, Integer currentUserId) { - User currentUser = userMapper.selectById(currentUserId); - if (!currentUser.getIsAdmin()) { - return null; + private List<String> checkUserInfo(User user) { + List<String> msgs = new ArrayList<>(); + //������������ + if (!RegexUtils.checkAccount(user.getAccount())) { + msgs.add("���������������������"); } - return userMapper.selectById(userId); + //������������ + if (!RegexUtils.checkPassword(user.getPassword())) { + msgs.add("���������������������"); + } + //������������ + if (user.getEmail() != null) { + if (!RegexUtils.checkEmail(user.getEmail())) { + msgs.add("���������������������"); + } + } + //��������������� + if (user.getMobile() != null) { + if (!RegexUtils.checkMobile(user.getMobile())) { + msgs.add("������������������������"); + } + } + return msgs; } } diff --git a/screen-api/src/main/resources/mapper/UserMapper.xml b/screen-api/src/main/resources/mapper/UserMapper.xml index 58ed1f3..bcbc3fb 100644 --- a/screen-api/src/main/resources/mapper/UserMapper.xml +++ b/screen-api/src/main/resources/mapper/UserMapper.xml @@ -19,4 +19,13 @@ <result column="is_delete" property="isDelete"/> </resultMap> + <select id="selectUsers" resultType="java.util.Map"> + SELECT id,account,user_name userName,email,mobile,wechat FROM `user` WHERE + <if test="orgId!=null"> + organization_id = #{orgId} + </if> + <if test="userId!=null"> + id = #{userId} + </if> + </select> </mapper> \ No newline at end of file diff --git a/screen-common/src/main/java/com/moral/util/RegexUtils.java b/screen-common/src/main/java/com/moral/util/RegexUtils.java new file mode 100644 index 0000000..3f2b70b --- /dev/null +++ b/screen-common/src/main/java/com/moral/util/RegexUtils.java @@ -0,0 +1,184 @@ +package com.moral.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RegexUtils { + + /** + * ��������������� + * + * @param account ������������������������������������������ ������ , ��������� , ������������������������3 ��� 20 ������ + * @return ���������������true���������������������false + */ + public static boolean checkAccount(String account) { + String regex = "[a-zA-Z]\\w{3,19}"; + return account.matches(regex); + } + + /** + * ������������ + * + * @param password ������������������ ������ , ��������� , ������������������������3 ��� 20 ������ + * @return ���������������true���������������������false + */ + public static boolean checkPassword(String password) { + String regex = "[0-9a-zA-Z_]\\w{3,19}"; + return password.matches(regex); + } + + /** + * ������Email + * + * @param email email������������������zhangsan@zuidaima.com���zhangsan@xxx.com.cn���xxx��������������������� + * @return ������������������true���������������������false + */ + public static boolean checkEmail(String email) { + String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?"; + return Pattern.matches(regex, email); + } + + /** + * ��������������������� + * + * @param idCard ���������������������15������18������������������������������������������ + * @return ������������������true���������������������false + */ + public static boolean checkIdCard(String idCard) { + String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}"; + return Pattern.matches(regex, idCard); + } + + /** + * ������������������ + * + * @param mobile ������������11������1������ + * @return ������������������true���������������������false + */ + public static boolean checkMobile(String mobile) { + String regex = "^1[0-9]{10}$"; + return Pattern.matches(regex, mobile); + } + + /** + * ������������������������ + * + * @param phone ������������������������������������������������������ + ������������������������ + ���������������������+8602085588447 + * <p><b>������������������ ������ ���</b>��������������������������������������������������������������������������������������� 0 ��� 9 ��������������������������� + * ���������������������������������������������������������</p> + * <p><b>���������������������������</b>��������������������������������� 0 ��� 9 ������������������������������������������������������ + * ������������������������������������������������������������������������������</p> + * <p><b>���������������</b>������������ 0 ��� 9 ������������������������ </p> + * @return ������������������true���������������������false + */ + public static boolean checkPhone(String phone) { + String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$"; + return Pattern.matches(regex, phone); + } + + /** + * ��������������������������������������� + * + * @param digit ���������������0-9��������������� + * @return ������������������true���������������������false + */ + public static boolean checkDigit(String digit) { + String regex = "\\-?[1-9]\\d+"; + return Pattern.matches(regex, digit); + } + + /** + * ������������������������������������������������������������ + * + * @param decimals ���������������0-9���������������������������1.23���233.30 + * @return ������������������true���������������������false + */ + public static boolean checkDecimals(String decimals) { + String regex = "\\-?[1-9]\\d+(\\.\\d+)?"; + return Pattern.matches(regex, decimals); + } + + /** + * ������������������ + * + * @param blankSpace ���������������������������������\t���\n���\r���\f���\x0B + * @return ������������������true���������������������false + */ + public static boolean checkBlankSpace(String blankSpace) { + String regex = "\\s+"; + return Pattern.matches(regex, blankSpace); + } + + /** + * ������������ + * + * @param chinese ������������ + * @return ������������������true���������������������false + */ + public static boolean checkChinese(String chinese) { + String regex = "^[\u4E00-\u9FA5]+$"; + return Pattern.matches(regex, chinese); + } + + /** + * ��������������������������� + * + * @param birthday ������������������1992-09-03������1992.09.03 + * @return ������������������true���������������������false + */ + public static boolean checkBirthday(String birthday) { + String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}"; + return Pattern.matches(regex, birthday); + } + + /** + * ������URL������ + * + * @param url ���������http://blog.csdn.net:80/xyang81/article/details/7705960? ��� http://www.csdn.net:80 + * @return ������������������true���������������������false + */ + public static boolean checkURL(String url) { + String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?"; + return Pattern.matches(regex, url); + } + + /** + * <pre> + * ������������ URL ������������ + * </pre> + * + * @param url + * @return + */ + public static String getDomain(String url) { + Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE); + // ��������������������� + // Pattern p=Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE); + Matcher matcher = p.matcher(url); + matcher.find(); + return matcher.group(); + } + + /** + * ������������������������ + * + * @param postcode ������������ + * @return ������������������true���������������������false + */ + public static boolean checkPostcode(String postcode) { + String regex = "[1-9]\\d{5}"; + return Pattern.matches(regex, postcode); + } + + /** + * ������IP������(������������������������������192.168.1.1���127.0.0.1���������������IP������������) + * + * @param ipAddress IPv4������������ + * @return ������������������true���������������������false + */ + public static boolean checkIpAddress(String ipAddress) { + String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))"; + return Pattern.matches(regex, ipAddress); + } + +} \ No newline at end of file -- Gitblit v1.8.0