Search in sources :

Example 16 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken 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)

Example 17 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project fruit-manage by liuzhaozhao.

the class ShiroDbRealm method doGetAuthenticationInfo.

// @Override
// public void setCacheManager(CacheManager cacheManager) {
// super.setCacheManager(cacheManager);
// // ShiroCache.setCacheManager(cacheManager);
// }
/**
 * 身份证认证
 *
 * @param authenticationToken
 * @return
 * @throws AuthenticationException
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
    String username = usernamePasswordToken.getUsername();
    User user = User.dao.getUser(username);
    SimpleAuthenticationInfo authenticationInfo = null;
    if (user != null) {
        authenticationInfo = new SimpleAuthenticationInfo(user, user.getPass(), getName());
    // authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(name
    // + user.get("salt")));
    }
    return authenticationInfo;
}
Also used : User(com.fruit.manage.model.User) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 18 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project Ganster-CMS by Gangster-trio.

the class LoginController method login.

@PostMapping("/login")
public Message login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
    logger.info("用户" + userName + "进行登录");
    UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
    // token.setRememberMe(true);
    Message message = new Message();
    Subject subject = SecurityUtils.getSubject();
    try {
        subject.login(token);
        message.setCode(100);
        message.setMsg("ok");
    } catch (Exception e) {
        message.setCode(120);
        message.setMsg("抱歉,信息错误");
        return message;
    }
    return message;
}
Also used : Message(com.ganster.cms.admin.dto.Message) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 19 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project airavata by apache.

the class JDBCUserStore method authenticate.

@Override
public boolean authenticate(String userName, Object credentials) throws UserStoreException {
    AuthenticationToken authenticationToken = new UsernamePasswordToken(userName, passwordDigester.getPasswordHashValue((String) credentials));
    AuthenticationInfo authenticationInfo;
    try {
        authenticationInfo = jdbcRealm.getAuthenticationInfo(authenticationToken);
        return authenticationInfo != null;
    } catch (AuthenticationException e) {
        log.debug(e.getLocalizedMessage(), e);
        return false;
    }
}
Also used : AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) AuthenticationException(org.apache.shiro.authc.AuthenticationException) AuthenticationInfo(org.apache.shiro.authc.AuthenticationInfo) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 20 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project shiro by apache.

the class LoginController method onSubmit.

protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd, BindException errors) throws Exception {
    LoginCommand command = (LoginCommand) cmd;
    UsernamePasswordToken token = new UsernamePasswordToken(command.getUsername(), command.getPassword());
    try {
        SecurityUtils.getSubject().login(token);
    } catch (AuthenticationException e) {
        log.debug("Error authenticating.", e);
        errors.reject("error.invalidLogin", "The username or password was not correct.");
    }
    if (errors.hasErrors()) {
        return showForm(request, response, errors);
    } else {
        return new ModelAndView(getSuccessView());
    }
}
Also used : AuthenticationException(org.apache.shiro.authc.AuthenticationException) ModelAndView(org.springframework.web.servlet.ModelAndView) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Aggregations

UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)114 Subject (org.apache.shiro.subject.Subject)50 Test (org.junit.Test)30 AuthenticationException (org.apache.shiro.authc.AuthenticationException)28 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)27 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)17 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Test (org.testng.annotations.Test)11 LockedAccountException (org.apache.shiro.authc.LockedAccountException)10 IncorrectCredentialsException (org.apache.shiro.authc.IncorrectCredentialsException)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 UnknownAccountException (org.apache.shiro.authc.UnknownAccountException)7 DelegatingSubject (org.apache.shiro.subject.support.DelegatingSubject)7 Session (org.apache.shiro.session.Session)6 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4