Search in sources :

Example 1 with User

use of com.alibaba.otter.canal.admin.model.User in project canal by alibaba.

the class UserServiceImpl method find4Login.

public User find4Login(String username, String password) {
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
        return null;
    }
    User user = User.find.query().where().eq("username", username).findOne();
    if (user == null) {
        throw new ServiceException("user:" + username + " auth failed!");
    }
    try {
        byte[] pass = SecurityUtil.scramble411(password.getBytes(), seeds);
        if (!SecurityUtil.scrambleServerAuth(pass, SecurityUtil.hexStr2Bytes(user.getPassword()), seeds)) {
            throw new ServiceException("user:" + user.getName() + " passwd incorrect!");
        }
    } catch (NoSuchAlgorithmException e) {
        throw new ServiceException("user:" + user.getName() + " auth failed!");
    }
    user.setPassword("");
    return user;
}
Also used : User(com.alibaba.otter.canal.admin.model.User) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with User

use of com.alibaba.otter.canal.admin.model.User in project canal by alibaba.

the class WebConfig method addInterceptors.

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new HandlerInterceptor() {

        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
            httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
            httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Token");
            httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
            httpServletResponse.setHeader("Access-Control-Max-Age", String.valueOf(3600 * 24));
            if (HttpMethod.OPTIONS.toString().equals(httpServletRequest.getMethod())) {
                httpServletResponse.setStatus(HttpStatus.NO_CONTENT.value());
                return false;
            }
            return true;
        }
    }).addPathPatterns("/api/**");
    registry.addInterceptor(new HandlerInterceptor() {

        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            String token = httpServletRequest.getHeader("X-Token");
            boolean valid = false;
            if (token != null) {
                User user = UserController.loginUsers.getIfPresent(token);
                if (user != null) {
                    valid = true;
                    httpServletRequest.setAttribute("user", user);
                    httpServletRequest.setAttribute("token", token);
                }
            }
            if (!valid) {
                BaseModel baseModel = BaseModel.getInstance(null);
                baseModel.setCode(50014);
                baseModel.setMessage("Expired token");
                ObjectMapper mapper = new ObjectMapper();
                String json = mapper.writeValueAsString(baseModel);
                try {
                    httpServletResponse.setContentType("application/json;charset=UTF-8");
                    PrintWriter out = httpServletResponse.getWriter();
                    out.print(json);
                } catch (Throwable e) {
                    throw new RuntimeException(e);
                }
                return false;
            }
            return true;
        }
    }).addPathPatterns("/api/**").excludePathPatterns("/api/**/config/server_polling").excludePathPatterns("/api/**/config/instances_polling").excludePathPatterns("/api/**/config/instance_polling/**").excludePathPatterns("/api/**/user/login").excludePathPatterns("/api/**/user/logout").excludePathPatterns("/api/**/user/info");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.alibaba.otter.canal.admin.model.User) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) BaseModel(com.alibaba.otter.canal.admin.model.BaseModel) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter)

Example 3 with User

use of com.alibaba.otter.canal.admin.model.User in project canal by alibaba.

the class UserServiceImpl method update.

public void update(User user) {
    User userTmp = User.find.query().where().eq("username", user.getUsername()).findOne();
    if (userTmp == null) {
        throw new ServiceException();
    }
    try {
        byte[] pass = SecurityUtil.scramble411(user.getOldPassword().getBytes(), seeds);
        if (!SecurityUtil.scrambleServerAuth(pass, SecurityUtil.hexStr2Bytes(userTmp.getPassword()), seeds)) {
            throw new ServiceException("old passwd is unmatch");
        }
        user.setId(userTmp.getId());
        user.setPassword(SecurityUtil.scrambleGenPass(user.getPassword().getBytes()));
    } catch (NoSuchAlgorithmException e) {
        throw new ServiceException("passwd process failed");
    }
    user.update("username", "nn:password");
}
Also used : User(com.alibaba.otter.canal.admin.model.User) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 4 with User

use of com.alibaba.otter.canal.admin.model.User in project canal by alibaba.

the class UserController method login.

/**
 * 用户登录
 *
 * @param user 账号密码
 * @param env 环境变量
 * @return token
 */
@PostMapping(value = "/login")
public BaseModel<Map<String, String>> login(@RequestBody User user, @PathVariable String env) {
    User loginUser = userService.find4Login(user.getUsername(), user.getPassword());
    if (loginUser != null) {
        Map<String, String> tokenResp = new HashMap<>();
        String token = UUID.randomUUID().toString();
        loginUsers.put(token, loginUser);
        tokenResp.put("token", token);
        return BaseModel.getInstance(tokenResp);
    } else {
        BaseModel<Map<String, String>> model = BaseModel.getInstance(null);
        model.setCode(40001);
        model.setMessage("Invalid username or password");
        return model;
    }
}
Also used : User(com.alibaba.otter.canal.admin.model.User) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with User

use of com.alibaba.otter.canal.admin.model.User in project canal by alibaba.

the class UserController method info.

/**
 * 获取用户信息
 *
 * @param token token
 * @param env 环境变量
 * @return 用户信息
 */
@GetMapping(value = "/info")
public BaseModel<User> info(@RequestParam String token, @PathVariable String env) {
    User user = loginUsers.getIfPresent(token);
    if (user != null) {
        return BaseModel.getInstance(user);
    } else {
        BaseModel<User> model = BaseModel.getInstance(null);
        model.setCode(50014);
        model.setMessage("Invalid token");
        return model;
    }
}
Also used : User(com.alibaba.otter.canal.admin.model.User) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

User (com.alibaba.otter.canal.admin.model.User)5 ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 BaseModel (com.alibaba.otter.canal.admin.model.BaseModel)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)1