当前位置: 首页>>代码示例>>Java>>正文


Java AuthenticationException类代码示例

本文整理汇总了Java中org.apache.shiro.authc.AuthenticationException的典型用法代码示例。如果您正苦于以下问题:Java AuthenticationException类的具体用法?Java AuthenticationException怎么用?Java AuthenticationException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


AuthenticationException类属于org.apache.shiro.authc包,在下文中一共展示了AuthenticationException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: onLoginFailure

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
@Override
protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException ae, ServletRequest request,
                                 ServletResponse response) {

    final OAuthResponse oAuthResponse;
    try {
        oAuthResponse = OAuthRSResponse.errorResponse(401)
                .setError(OAuthError.ResourceResponse.INVALID_TOKEN)
                .setErrorDescription(ae.getMessage())
                .buildJSONMessage();

        com.monkeyk.os.web.WebUtils.writeOAuthJsonResponse((HttpServletResponse) response, oAuthResponse);

    } catch (OAuthSystemException e) {
        LOGGER.error("Build JSON message error", e);
        throw new IllegalStateException(e);
    }


    return false;
}
 
开发者ID:monkeyk,项目名称:oauth2-shiro,代码行数:22,代码来源:OAuth2Filter.java

示例2: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
/**
 * 认证回调函数,登录时调用.
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
		AuthenticationToken authcToken) throws AuthenticationException {
	UsernamePassword2Token token = (UsernamePassword2Token) authcToken;
	String username = token.getUsername();
	if (username == null || null == username) {
		throw new AccountException(
				"Null usernames are not allowed by this realm.");
	}
	User entity = new User();
	entity.setEmail(username);
	entity.setStatus(Constant.STATUS_ENABLED);
	entity = (User) service.iUserService.select(entity);
	if (null == entity) {
		throw new UnknownAccountException("No account found for user ["
				+ username + "]");
	}
	byte[] key = Encode.decodeHex(entity.getRandom());
	return new SimpleAuthenticationInfo(new Shiro(entity.getId(),
			entity.getEmail(), entity.getName()), entity.getPassword(),
			ByteSource.Util.bytes(key), getName());
}
 
开发者ID:jiangzongyao,项目名称:kettle_support_kettle8.0,代码行数:26,代码来源:Authorizing2Realm.java

示例3: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationException; //导入依赖的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

示例4: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException {
    String token = (String) auth.getCredentials();
    Cache<String, String> authCache = CacheController.getAuthCache();
    if (! authCache.containsKey(token)) {
        // get user info from database
        int uid = JWTUtil.getUid(token);
        UserEntity userEntity = userService.getUserByUid(uid);
        authCache.put(token, String.valueOf(userEntity.getPassword()));
    }

    String secret = authCache.get(token);
    if (!JWTUtil.decode(token, secret)) {
        throw new AuthenticationException("Token invalid");
    }

    return new SimpleAuthenticationInfo(token, token, "jwt_realm");
}
 
开发者ID:Eagle-OJ,项目名称:eagle-oj-api,代码行数:19,代码来源:Realm.java

示例5: login

import org.apache.shiro.authc.AuthenticationException; //导入依赖的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: onLoginFailure

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
@Override
protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.setContentType("application/json;charset=utf-8");
    try {
        //处理登录失败的异常
        Throwable throwable = e.getCause() == null ? e : e.getCause();
        R r = R.error(HttpStatus.SC_UNAUTHORIZED, throwable.getMessage());

        String json = new Gson().toJson(r);
        httpResponse.getWriter().print(json);
    } catch (IOException e1) {

    }

    return false;
}
 
开发者ID:zhaoqicheng,项目名称:renren-fast,代码行数:18,代码来源:OAuth2Filter.java

示例7: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
/**
 * 用户认证-验证用户是否登录、用户名密码是否匹配
 */
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	logger.info(">>> 【用户认证】token = {}", token);
	String userName = (String)token.getPrincipal();
	AdminUser user = getPrincipalService().getPrincipalObject(userName);
       if(user == null) {
           throw new UnknownAccountException("Unknown account: " + userName);//没找到帐号
       }
       if(AdminUserStatusEnum.ADMIN_USER_STATUS_DISABLED.getStatusCode().equals(user.getStatus())) {
           throw new LockedAccountException("Account[" + userName + "] has been locked!"); //帐号锁定
       }
       //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配
       SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
               user.getUserName(), //用户名
               user.getPassword(), //密码
               ByteSource.Util.bytes(user.getPasswordSalt()),//salt
               getName()  //realm name
       );
       return authenticationInfo;
}
 
开发者ID:penggle,项目名称:xproject,代码行数:23,代码来源:AdminUserRealm.java

示例8: onLoginFailure

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
@Override
protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
    if (!((HttpServletRequest)request).getRequestURL().toString().endsWith(".json")) {
        setFailureAttribute(request, e);
        return true;
    }
    try {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        String message = e.getClass().getSimpleName();
        if ("IncorrectCredentialsException".equals(message)
                || "UnknownAccountException".equals(message)
                ) {
            out.println("{\"code\":-100010,\"info\":\"账号或密码错误\"}");
        }else if("ExcessiveAttemptsException".equals(message)){
            out.println("{\"code\":-100020,\"info\":\"密码错误次数超过限制,请10分钟后重试!\"}");
        }else if("LockedAccountException".equals(message)){
            out.println("{\"code\":-100030,\"info\":\"账号已停用!\"}");
        } else {
            out.println("{\"code\":-100500,\"info\":\"未知错误\"}");
        }
        out.flush();
        out.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return false;
}
 
开发者ID:liaojiacan,项目名称:zkAdmin,代码行数:31,代码来源:LoginFilter.java

示例9: testHelloWorld

import org.apache.shiro.authc.AuthenticationException; //导入依赖的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

示例10: isAccessAllowed

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
@Override
protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    String token = jwtHelper.getToken(request);
    String username = jwtHelper.getUsernameFromToken(token);
    StatelessToken accessToken = new StatelessToken(username, token);
    try {
        getSubject(servletRequest, servletResponse).login(accessToken);
    } catch (AuthenticationException e) {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        objectMapper.writeValue(response.getWriter(), Result.fail(ResultCode.UNAUTHORIZED));
        return false;
    }
    getSubject(servletRequest, servletResponse).isPermitted(request.getRequestURI());
    return true;
}
 
开发者ID:YupaiTS,项目名称:docs-manage,代码行数:18,代码来源:StatelessAuthcFilter.java

示例11: onLoginFailure

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
@Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException ae, ServletRequest request,
                                     ServletResponse response) {
//        OAuth2Token oAuth2Token = (OAuth2Token) token;

        final OAuthResponse oAuthResponse;
        try {
            oAuthResponse = OAuthRSResponse.errorResponse(401)
                    .setError(OAuthError.ResourceResponse.INVALID_TOKEN)
                    .setErrorDescription(ae.getMessage())
                    .buildJSONMessage();

            com.monkeyk.os.web.WebUtils.writeOAuthJsonResponse((HttpServletResponse) response, oAuthResponse);

        } catch (OAuthSystemException e) {
            logger.error("Build JSON message error", e);
            throw new IllegalStateException(e);
        }


        return false;
    }
 
开发者ID:monkeyk,项目名称:oauth2-shiro-redis,代码行数:23,代码来源:OAuth2Filter.java

示例12: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationException; //导入依赖的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

示例13: login

import org.apache.shiro.authc.AuthenticationException; //导入依赖的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

示例14: login

import org.apache.shiro.authc.AuthenticationException; //导入依赖的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

示例15: hello

import org.apache.shiro.authc.AuthenticationException; //导入依赖的package包/类
@RequestMapping(value ="/hello")
@ResponseBody
public String hello(){
	Subject subject = SecurityUtils.getSubject();
	UsernamePasswordToken token = new UsernamePasswordToken("zhansan", "123456");
	//--4. 登录,即身份验证
	try {
		subject.login(token);
	} catch (AuthenticationException e) {
		e.printStackTrace();
	}
	//System.out.println(subject.isAuthenticated());
	//System.out.println(subject.getPrincipal());
	//-- 6. 退出
	System.out.println(subject.isAuthenticated());
	subject.logout();
	return "hello";
}
 
开发者ID:ranji1221,项目名称:clemon,代码行数:19,代码来源:OltuAuthorizeController.java


注:本文中的org.apache.shiro.authc.AuthenticationException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。