Search in sources :

Example 1 with User

use of com.luoxiao.model.User in project ssm_shiro_blog by Mandelo.

the class MainController method updateRoles.

/**
 * 修改角色roles,并保存
 *
 * @param userId
 * @return
 */
@ResponseBody
@RequestMapping(value = "/updateRoles", method = RequestMethod.GET)
public UserRoleTemp updateRoles(@RequestParam(value = "userId") Integer userId, @RequestParam(value = "roleStr") String roleStr) {
    UserRoleTemp u = new UserRoleTemp();
    /* System.out.println(roleStr); */
    // 根据用户userId删除所有RoleId
    userRoleService.deleteById(userId);
    // 遍历角色名
    String[] roleNames = roleStr.split(",");
    for (int i = 0; i < roleNames.length; i++) {
        Integer roleId = (roleService.getIdByRoleName(roleNames[i]));
        // 插入新的数据到UserRole表
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        userRoleService.insert(userRole);
    }
    // 重新获取结果
    User user = userService.selectById(userId);
    List<String> roles = userExtendService.getRoles(user.getUsername());
    u.setId(userId);
    u.setRoles(roles);
    u.setUsername(user.getUsername());
    return u;
}
Also used : User(com.luoxiao.model.User) UserRoleTemp(com.luoxiao.model.UserRoleTemp) UserRole(com.luoxiao.model.UserRole) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with User

use of com.luoxiao.model.User in project ssm_shiro_blog by Mandelo.

the class MainController method submitBlog.

// 提交博客
@RequestMapping(value = "/user/submitBlog", method = RequestMethod.POST)
public ModelAndView submitBlog(Blog blog, HttpSession session) {
    ModelAndView mv = new ModelAndView();
    try {
        blog.setCreateTime(new Date());
        User loginUser = (User) session.getAttribute("loginUser");
        blog.setUserId(loginUser.getId());
        blog.setAuthor(loginUser.getUsername());
        blogService.insert(blog);
        mv.setViewName("success/blogSubmitSuccess");
    } catch (Exception e) {
        mv.setViewName("error/errorPage");
        e.printStackTrace();
    }
    return mv;
}
Also used : User(com.luoxiao.model.User) ModelAndView(org.springframework.web.servlet.ModelAndView) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with User

use of com.luoxiao.model.User in project ssm_shiro_blog by Mandelo.

the class MainController method roleManage.

/**
 * 用户角色管理
 *
 * @param userId
 * @return
 */
@ResponseBody
@RequestMapping(value = "/getRoles")
public UserRoleTemp roleManage(@RequestParam(value = "userId") Integer userId) {
    // System.out.println(userId);
    UserRoleTemp u = new UserRoleTemp();
    User user = userService.selectById(userId);
    List<String> roles = userExtendService.getRoles(user.getUsername());
    u.setId(userId);
    u.setRoles(roles);
    u.setUsername(user.getUsername());
    return u;
}
Also used : User(com.luoxiao.model.User) UserRoleTemp(com.luoxiao.model.UserRoleTemp) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with User

use of com.luoxiao.model.User in project ssm_shiro_blog by Mandelo.

the class MainController method validateUser.

/**
 * ajax验证用户名密码
 *
 * @return
 */
@ResponseBody
@RequestMapping(value = "/validatePassword")
public String validateUser(String username) {
    User _user = userService.selectByUsername(username);
    String _password = _user.getPassword();
    if (_password == null) {
        return "";
    } else {
        return _password;
    }
}
Also used : User(com.luoxiao.model.User) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with User

use of com.luoxiao.model.User in project ssm_shiro_blog by Mandelo.

the class MainController method login.

/**
 * 登录功能
 *
 * @param user
 * @param model
 * @return
 */
@RequestMapping(value = "/user/login", method = RequestMethod.POST)
public String login(User user, HttpSession session, Model model) {
    UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
    Subject subject = SecurityUtils.getSubject();
    subject.login(token);
    User loginUser = userService.selectByUsername(user.getUsername());
    session.setAttribute("loginUser", loginUser);
    // System.out.println(loginUser);
    return "/loginSuccess";
}
Also used : User(com.luoxiao.model.User) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (com.luoxiao.model.User)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 UserRoleTemp (com.luoxiao.model.UserRoleTemp)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ModelAndView (org.springframework.web.servlet.ModelAndView)2 UserRole (com.luoxiao.model.UserRole)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)1 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)1 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)1 Subject (org.apache.shiro.subject.Subject)1