當前位置: 首頁>>代碼示例>>Java>>正文


Java UsernamePasswordToken類代碼示例

本文整理匯總了Java中org.apache.shiro.authc.UsernamePasswordToken的典型用法代碼示例。如果您正苦於以下問題:Java UsernamePasswordToken類的具體用法?Java UsernamePasswordToken怎麽用?Java UsernamePasswordToken使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


UsernamePasswordToken類屬於org.apache.shiro.authc包,在下文中一共展示了UsernamePasswordToken類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: queryForAuthenticationInfo

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
/**
 * Builds an {@link AuthenticationInfo} object by querying the active directory LDAP context for the
 * specified username.
 */
@Override
protected AuthenticationInfo queryForAuthenticationInfo(
        AuthenticationToken token, LdapContextFactory ldapContextFactory) throws NamingException {

    final UsernamePasswordToken upToken = ensureUsernamePasswordToken(token);
    final String userDn = findUserDn(ldapContextFactory, upToken.getUsername());

    LdapContext ctx = null;
    try {
        // Binds using the username and password provided by the user.
        ctx = ldapContextFactory.getLdapContext(userDn, upToken.getPassword());
    } finally {
        LdapUtils.closeContext(ctx);
    }
    return buildAuthenticationInfo(upToken.getUsername(), upToken.getPassword());
}
 
開發者ID:line,項目名稱:centraldogma,代碼行數:21,代碼來源:SearchFirstActiveDirectoryRealm.java

示例2: login

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@ModelAttribute("login")
private MsgTO login(@RequestBody MsgTO msgTo) {
	Long userId = msgTo.getUserId();
	String password = msgTo.getPassword();
	SysUserEntity user = ((SysUserService) SpringContextUtils.getBean("sysUserService")).queryObject(userId);
	if(user==null){
		throw new RRException("用戶id不存在");
	}
	try {
		Subject subject = ShiroUtils.getSubject();
		// sha256加密
		password = new Sha256Hash(password).toHex();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), password);
		subject.login(token);
	} catch (Exception e) {
		throw new RRException("登錄失敗");
	}
	msgTo.setUser(user);
	return msgTo;
}
 
開發者ID:gyp220203,項目名稱:renren-msg,代碼行數:21,代碼來源:MsgSendController.java

示例3: ClothoRealm

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@Inject
public ClothoRealm(CredentialStore store, RolePermissionResolver roleResolver) {
    super();
    
    //XXX: up number of iterations
    HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(Sha256Hash.ALGORITHM_NAME);
    matcher.setStoredCredentialsHexEncoded(false);
    
    this.store = store;
    setAuthenticationTokenClass(UsernamePasswordToken.class);
    setCredentialsMatcher(matcher);

    setRolePermissionResolver(roleResolver);

    setUpRealm();
}
 
開發者ID:CIDARLAB,項目名稱:clotho3crud,代碼行數:17,代碼來源:ClothoRealm.java

示例4: doGetAuthenticationInfo

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
		//UsernamePasswordToken對象用來存放提交的登錄信息
        UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;

        log.info("驗證當前Subject時獲取到token為:" + ReflectionToStringBuilder.toString(token, ToStringStyle.MULTI_LINE_STYLE)); 
//        return new SimpleAuthenticationInfo("hsjhsj","8e24137dee97c9bbddb9a0cd6e043be4" , getName());
        return new SimpleAuthenticationInfo("hsjhsj","" , getName());
        //查出是否有此用戶
//        TbUser user=null;
//        if(user!=null){
            // 若存在,將此用戶存放到登錄認證info中,無需自己做密碼對比,Shiro會為我們進行密碼對比校驗
//            return new SimpleAuthenticationInfo(user.getUsername(), , getName());
//        }
//        return null;
	}
 
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:17,代碼來源:ShiroRealm.java

示例5: login

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
/**
 * 用戶登錄
 * @param request
 * @param user
 * @param model
 * @return
 */
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String login(HttpServletRequest request, AdminUser user, Model model) {

    if (StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){
        request.setAttribute("msg","用戶名或者密碼不能為空!");
        return "login";
    }
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword());
    try {
        subject.login(token);
        return "redirect:/initPage";
    }catch (LockedAccountException lae) {
        token.clear();
        request.setAttribute("msg", "用戶已經被鎖定不能登錄,請與管理員聯係!");
        return "login";
    } catch (AuthenticationException e) {
        token.clear();
        request.setAttribute("msg", "用戶或密碼不正確!");
        return "login";
    }
}
 
開發者ID:ChinaLHR,項目名稱:JavaQuarkBBS,代碼行數:30,代碼來源:PageController.java

示例6: dashboard

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@RequestMapping(value = "/login", method = {
        RequestMethod.POST})
public String dashboard(ModelMap map, Admin admin) {
    String error = null;
    UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(), admin.getPassword());
    token.setRememberMe(false);
    try {
        SecurityUtils.getSubject().login(token);
        return "redirect:/video/all";
    } catch (UnknownAccountException uae) {
        error = "用戶名錯誤!";
    } catch (IncorrectCredentialsException ice) {
        error = "密碼錯誤!";
    } catch (LockedAccountException lae) {
        error = "用戶被鎖定!";
    }
    map.addAttribute("error", error);
    return "login.ftl";
}
 
開發者ID:melonlee,項目名稱:LazyAdmin,代碼行數:20,代碼來源:AuthController.java

示例7: login

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@RequestMapping(value = "/tlogin", method = RequestMethod.POST)
public String login(String username, String password, HttpServletRequest request) {

    //String validateCode = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
    // if (StringUtils.isNotBlank(checkcode) && checkcode.equals(validateCode)) {
    // 使用shiri方式
    // 獲得當前對象的狀態:未認證
    Subject subject = SecurityUtils.getSubject();
    // 用戶名密碼令牌對象
    AuthenticationToken token = new UsernamePasswordToken(username,
            password);
    try {
        subject.login(token);
    } catch (Exception e) {
        e.printStackTrace();
        return "login";
    }
    User user = (User) subject.getPrincipal();
    // user放入session
    request.getSession().setAttribute("loginUser", user);
    return "index";
}
 
開發者ID:mmdsyl,項目名稱:BLOG-Microservice,代碼行數:23,代碼來源:TestController.java

示例8: login

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@RequestMapping(value = "login", method = RequestMethod.POST)
public String login(@ModelAttribute("formDto") LoginDto formDto, BindingResult errors) {

    UsernamePasswordToken token = formDto.token();
    token.setRememberMe(false);

    try {
        SecurityUtils.getSubject().login(token);
    } catch (Exception e) {
        LOG.debug("Error authenticating.", e);
        errors.rejectValue("username", null, "The username or password was not correct.");
        return "login";
    }

    return "redirect:index";
}
 
開發者ID:monkeyk,項目名稱:oauth2-shiro,代碼行數:17,代碼來源:ShiroController.java

示例9: submitLogin

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
protected boolean submitLogin() throws ServletException, IOException {
    if (isSubmitLogin()) {
        //login flow
        try {
            UsernamePasswordToken token = createUsernamePasswordToken();
            SecurityUtils.getSubject().login(token);

            LOG.debug("Submit login successful");
            this.userFirstLogged = true;
            return false;
        } catch (Exception ex) {
            //login failed
            LOG.debug("Login failed, back to login page too", ex);

            final HttpServletRequest request = oauthRequest.request();
            request.setAttribute("oauth_login_error", true);
            request.getRequestDispatcher(OAUTH_LOGIN_VIEW)
                    .forward(request, response);
            return true;
        }
    }
    return false;
}
 
開發者ID:monkeyk,項目名稱:oauth2-shiro,代碼行數:24,代碼來源:AbstractAuthorizeHandler.java

示例10: testHelloWorld

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@Test
public void testHelloWorld() {
    //1、獲取 SecurityManager 工廠,此處使用 Ini 配置文件初始化 SecurityManager
    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
    //2、得到 SecurityManager 實例 並綁定給 SecurityUtils

    SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);

    //3、得到 Subject 及創建用戶名/密碼身份驗證 Token(即用戶身份/憑證)
    Subject subject = SecurityUtils.getSubject();

    UsernamePasswordToken token = new UsernamePasswordToken("test", "234");

    try {
        //4、登錄,即身份驗證
        subject.login(token);
    } catch (AuthenticationException e) {
        //5、身份驗證失敗
    }
    Assert.assertEquals(true, subject.isAuthenticated()); //斷言用戶已經登錄
    //6、退出
    subject.logout();
}
 
開發者ID:easymall,項目名稱:easymall,代碼行數:25,代碼來源:LoginTest.java

示例11: login

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
private boolean login(Subject subject, HttpServletRequest request) {
    if("get".equalsIgnoreCase(request.getMethod())) {
        return false;
    }
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
        return false;
    }

    UsernamePasswordToken token = new UsernamePasswordToken(username, password);

    try {
        subject.login(token);
        return true;
    } catch (Exception e) {
        request.setAttribute("error", "登錄失敗:" + e.getClass().getName());
        return false;
    }
}
 
開發者ID:babymm,項目名稱:mumu,代碼行數:22,代碼來源:AuthorizeController.java

示例12: tryLogin

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
public boolean tryLogin(String email, String password, Boolean rememberMe) {
    org.apache.shiro.subject.Subject currentUser = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(email, password);
    token.setRememberMe(rememberMe);

    try {
        currentUser.login(token);
        System.out.println("User [" + currentUser.getPrincipal().toString() + "] logged in successfully.");
        // save username in the session
        currentUser.getSession().setAttribute("username", email);
        return true;
    } catch (UnknownAccountException uae) {
        System.out.println("There is no user with username of " + token.getPrincipal());
    } catch (IncorrectCredentialsException ice) {
        System.out.println("Password for account " + token.getPrincipal() + " was incorrect!");
    } catch (LockedAccountException lae) {
        System.out.println("The account for username " + token.getPrincipal() + " is locked.  " + "Please contact your administrator to unlock it.");
    }

    return false;
}
 
開發者ID:ETspielberg,項目名稱:bibliometrics,代碼行數:22,代碼來源:BibliometricReportRetrievalServlet.java

示例13: doGetAuthenticationInfo

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	// identify account to log to
	UsernamePasswordToken userPassToken = (UsernamePasswordToken) token;
	final String username = userPassToken.getUsername();
	if (username == null) {
		return null;
	}
	// read password hash and salt from db
	final User user = UserDAO.getUser(username);
	if (user == null) {
		return null;
	}
	// return salted credentials
	SaltedAuthenticationInfo info = new SaltedAuthInfo(username, user.getPassword(), user.getSalt());
	return info;
}
 
開發者ID:ETspielberg,項目名稱:bibliometrics,代碼行數:18,代碼來源:BibliometricsRealm.java

示例14: login

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@PostMapping(value = SUBPATH_LOGIN)
public ResponseEntity<UserDto> login(@RequestBody UserDto userDto,
                                     UriComponentsBuilder uriComponentsBuilder){
    HttpHeaders headers = ApplicationUtil.getHttpHeaders(uriComponentsBuilder,SUBPATH_LOGIN);
    logger.info("================userInfo================username: " + userDto.getUsername() + ",pw: " + userDto.getPassword());
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(userDto.getUsername(),userDto.getPassword());
    //User user = new User("root","root","root","root");
    //userDao.save(user);
    try{
        subject.login(token);
    } catch (AuthenticationException e){
        logger.error("======登錄失敗======");
        throw new ResultException(ErrorCode.USERNAMEORPASSWORD.getDesc(),ErrorCode.USERNAMEORPASSWORD);
    }
    UserDto loginUserDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("user");

    return new ResponseEntity<>(loginUserDto,headers, HttpStatus.OK);
}
 
開發者ID:ZhuXS,項目名稱:Spring-Shiro-Spark,代碼行數:20,代碼來源:AuthController.java

示例15: login

import org.apache.shiro.authc.UsernamePasswordToken; //導入依賴的package包/類
@RequestMapping(value="/login",method=RequestMethod.POST)
public ModelAndView login(User user, String captcha, HttpSession session,HttpServletRequest request) throws Exception{
	ModelAndView mv = new ModelAndView();
	
	String kaptchaExpected = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
	System.out.println(kaptchaExpected);
	Subject subject = SecurityUtils.getSubject();
	UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
	try{
		subject.login(token);
		mv.setViewName("redirect:/index.jsp");
	} catch (AuthenticationException e){
		mv.addObject("message", "login errors");
		mv.setViewName("redirect:/backend/login");
	} 
	return mv;
}
 
開發者ID:ranji1221,項目名稱:clemon,代碼行數:18,代碼來源:LoginController.java


注:本文中的org.apache.shiro.authc.UsernamePasswordToken類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。