use of mingzuozhibi.persist.core.User in project mzzb-server by mingzuozhibi.
the class UserController method findAll.
@Transactional
@PreAuthorize("hasRole('ADMIN')")
@GetMapping(value = "/api/users", produces = MEDIA_TYPE)
public String findAll() {
JSONArray result = new JSONArray();
dao.findAll(User.class).forEach(user -> {
result.put(user.toJSON());
});
if (LOGGER.isDebugEnabled()) {
debugRequest("[获取多个用户成功][用户数量={}]", result.length());
}
return objectResult(result);
}
use of mingzuozhibi.persist.core.User in project mzzb-server by mingzuozhibi.
the class UserController method getOne.
@Transactional
@PreAuthorize("hasRole('ADMIN')")
@GetMapping(value = "/api/users/{id}", produces = MEDIA_TYPE)
public String getOne(@PathVariable Long id) {
User user = dao.get(User.class, id);
if (user == null) {
if (LOGGER.isWarnEnabled()) {
warnRequest("[获取用户失败][指定的用户Id不存在][Id={}]", id);
}
return errorMessage("指定的用户Id不存在");
}
JSONObject result = user.toJSON();
if (LOGGER.isDebugEnabled()) {
debugRequest("[获取用户成功][用户信息={}]", result);
}
return objectResult(result);
}
use of mingzuozhibi.persist.core.User in project mzzb-server by mingzuozhibi.
the class UserController method addOne.
@Transactional
@PreAuthorize("hasRole('ADMIN')")
@PostMapping(value = "/api/users", produces = MEDIA_TYPE)
public String addOne(@JsonArg String username, @JsonArg String password, @JsonArg(defaults = "true") boolean enabled) {
if (dao.lookup(User.class, "username", username) != null) {
if (LOGGER.isInfoEnabled()) {
infoRequest("[创建用户失败][该同户名称已存在][用户名={}]", username);
}
return errorMessage("该同户名称已存在");
}
User user = new User(username, password, enabled);
dao.save(user);
JSONObject result = user.toJSON();
if (LOGGER.isInfoEnabled()) {
infoRequest("[创建用户成功][用户信息={}]", result);
}
return objectResult(result);
}
use of mingzuozhibi.persist.core.User in project mzzb-server by mingzuozhibi.
the class UserController method setOne.
@Transactional
@PreAuthorize("hasRole('ADMIN')")
@PutMapping(value = "/api/users/{id}", produces = MEDIA_TYPE)
public String setOne(@PathVariable Long id, @JsonArg String username, @JsonArg String password, @JsonArg boolean enabled) {
User user = dao.get(User.class, id);
if (user == null) {
if (LOGGER.isWarnEnabled()) {
warnRequest("[编辑用户失败][指定的用户Id不存在][Id={}]", id);
}
return errorMessage("指定的用户Id不存在");
}
if (LOGGER.isDebugEnabled()) {
JSONObject before = user.toJSON();
debugRequest("[编辑用户开始][修改前={}]", before);
}
user.setUsername(username);
user.setEnabled(enabled);
if (password != null && !password.isEmpty()) {
user.setPassword(password);
cleanAutoLogin(user);
}
JSONObject result = user.toJSON();
if (LOGGER.isDebugEnabled()) {
debugRequest("[编辑用户成功][修改后={}]", result);
}
return objectResult(result);
}
use of mingzuozhibi.persist.core.User in project mzzb-server by mingzuozhibi.
the class SecurityConfig method configure.
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/api/session/**").permitAll().antMatchers(HttpMethod.GET).permitAll().antMatchers("/api/**").hasRole("BASIC").and().anonymous().principal("Guest").authorities("NONE").and().exceptionHandling().accessDeniedHandler(customAccessDeniedHandler).authenticationEntryPoint(customAccessDeniedHandler).and().csrf().ignoringAntMatchers("/api/session/**").ignoringAntMatchers("/management/**").and().addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class);
Logger logger = LoggerFactory.getLogger(SecurityConfig.class);
logger.info("设置Security安全策略");
dao.execute(session -> {
String encode = new PassUtil().encode("admin", adminPassword);
User user = dao.lookup(User.class, "username", "admin");
if (user == null) {
user = new User("admin", encode, true);
user.getRoles().add("ROLE_ADMIN");
dao.save(user);
logger.info("初始化管理员用户");
} else {
if (!encode.equals(user.getPassword())) {
logger.info("更新管理员密码");
user.setPassword(encode);
}
if (!user.getRoles().contains("ROLE_BASIC")) {
logger.info("更新管理员权限");
user.getRoles().add("ROLE_BASIC");
}
if (!user.getRoles().contains("ROLE_ADMIN")) {
logger.info("更新管理员权限");
user.getRoles().add("ROLE_ADMIN");
}
if (!user.isEnabled()) {
logger.info("启用管理员用户");
user.setEnabled(true);
}
}
});
}
Aggregations