Java Code Examples for org.apache.shiro.subject.Subject#isRemembered()
The following examples show how to use
org.apache.shiro.subject.Subject#isRemembered() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoginController.java From scaffold-cloud with MIT License | 6 votes |
@RequestMapping(value = "/login") public String login() { final Object principal = UserUtil.getPrincipal(); if (null != principal) { final Subject subject = SecurityUtils.getSubject(); if (subject.isRemembered()) { final SysOperateBO operator = UserUtil.getOperatorFromSession(); if (null != operator && null != operator.getId()) { logger.info("当前登陆用户为:{}, 跳转到Index", operator.getRealName()); subject.getSession().setAttribute(SysConstants.SESSION_ATTRIBUTE_KEY_OPERATOR, operator); } } return "redirect:index"; } return "login"; }
Example 2
Source File: AuthenticatedFilter.java From mblog with GNU General Public License v3.0 | 6 votes |
@Override protected void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { Subject subject = SecurityUtils.getSubject(); if (subject.isAuthenticated() || subject.isRemembered()) { chain.doFilter(request, response); } else { WebUtils.saveRequest(request); String path = WebUtils.getContextPath((HttpServletRequest) request); String url = loginUrl; if (StringUtils.isNotBlank(path) && path.length() > 1) { url = path + url; } if (isAjaxRequest((HttpServletRequest) request)) { response.setContentType("application/json;charset=UTF-8"); response.getWriter().print(JSON.toJSONString(Result.failure("您还没有登录!"))); } else { response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(new Formatter().format(JS, url).toString()); } } }
Example 3
Source File: RegisterController.java From PhrackCTF-Platform-Team with Apache License 2.0 | 6 votes |
@RequestMapping(value = "/register",method = RequestMethod.GET) public ModelAndView doGetRegister() throws Exception { ModelAndView mv = new ModelAndView("register"); Subject currentUser = SecurityUtils.getSubject(); CommonUtils.setUserInfo(currentUser, userServices, teamServices,submissionServices,mv); CommonUtils.setControllerName(request, mv); if (currentUser.isAuthenticated()||currentUser.isRemembered()) { return new ModelAndView("redirect:/home"); } List<Countries> cts = countryServices.SelectAllCountry(); mv.addObject("country",cts); mv.setViewName("register"); return mv; }
Example 4
Source File: CommonUtils.java From PhrackCTF-Platform-Team with Apache License 2.0 | 6 votes |
public static Users setUserInfo(Subject currentUser,UserServices userServices,TeamServices teamServices,SubmissionServices submissionServices,ModelAndView mv) { if (currentUser==null) { return null; } if (currentUser.isRemembered()||currentUser.isAuthenticated()) { Users userobj = userServices.getUserByEmail((String)currentUser.getPrincipal()); Long userteam = userobj.getTeamid(); if (userteam==null) { mv.addObject("username_top", userobj.getUsername()); mv.addObject("score", userobj.getScore()); mv.addObject("usericon","user"); mv.addObject("rank", getUserrank(userobj,userServices,submissionServices)); } else { Teams teamobj = teamServices.getTeamById(userteam); long rank = getTeamrank(teamobj,teamServices,userServices,submissionServices); mv.addObject("username_top", teamobj.getName()); mv.addObject("score", teamobj.getScore()); mv.addObject("usericon","group"); mv.addObject("rank", rank); } return userobj; } return null; }
Example 5
Source File: LoginController.java From MultimediaDesktop with Apache License 2.0 | 5 votes |
@RequestMapping(value = "/user/remember") public @ResponseBody ShiroUser remember() { Subject subject = SecurityUtils.getSubject(); ShiroUser shiroUser = null; if (!subject.isAuthenticated() && subject.isRemembered()) { shiroUser = (ShiroUser) subject.getPrincipal(); } return shiroUser; }
Example 6
Source File: SessionInterceptor.java From ZTuoExchange_framework with MIT License | 5 votes |
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()); AdminService adminService = (AdminService) factory.getBean("adminService"); System.out.println(request.getContextPath()); Subject currentUser = SecurityUtils.getSubject(); //判断用户是通过记住我功能自动登录,此时session失效 if(!currentUser.isAuthenticated() && currentUser.isRemembered()){ try { Admin admin = adminService.findByUsername(currentUser.getPrincipals().toString()); //对密码进行加密后验证 UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(), admin.getPassword(),currentUser.isRemembered()); //把当前用户放入session currentUser.login(token); Session session = currentUser.getSession(); session.setAttribute(SysConstant.SESSION_ADMIN,admin); //设置会话的过期时间--ms,默认是30分钟,设置负数表示永不过期 session.setTimeout(30*60*1000L); }catch (Exception e){ //自动登录失败,跳转到登录页面 //response.sendRedirect(request.getContextPath()+"/system/employee/sign/in"); ajaxReturn(response, 4000, "unauthorized"); return false; } if(!currentUser.isAuthenticated()){ //自动登录失败,跳转到登录页面 ajaxReturn(response, 4000, "unauthorized"); return false; } } return true; }
Example 7
Source File: ShiroJwtVerifyingFilter.java From cassandra-reaper with Apache License 2.0 | 5 votes |
@Override protected boolean isAccessAllowed(ServletRequest req, ServletResponse res, Object mappedValue) throws Exception { Subject nonJwt = getSubject(req, res); return null != nonJwt.getPrincipal() && (nonJwt.isRemembered() || nonJwt.isAuthenticated()) ? true : getJwtUser(req).isPresent(); }
Example 8
Source File: SecurityComponent.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
@DirectMethod @Timed @ExceptionMetered public List<PermissionXO> getPermissions() { List<PermissionXO> permissions = null; Subject subject = securitySystem.getSubject(); if (subject != null && (subject.isAuthenticated() || subject.isRemembered())) { permissions = calculatePermissions(subject); } return permissions; }
Example 9
Source File: ResetPassController.java From PhrackCTF-Platform-Team with Apache License 2.0 | 5 votes |
/** * 这个是重设密码的方法 * * @return * @throws Exception */ @RequestMapping(value = "/resetpass", method = RequestMethod.GET) public ModelAndView Resetpass() throws Exception { ModelAndView mv = new ModelAndView("resetpass"); Subject currentUser = SecurityUtils.getSubject(); ArrayList<StatusMsg> states = new ArrayList<StatusMsg>(); CommonUtils.setUserInfo(currentUser, userServices, teamServices,submissionServices,mv); CommonUtils.setControllerName(request, mv); String token = request.getParameter("token"); if (token==null){ mv.setViewName("resetpass"); if (currentUser.isAuthenticated()||currentUser.isRemembered()) return new ModelAndView("redirect:/home"); } else { Passreset pr = passResetServices.getResetRecordBytoken(token); Date currenttime = new Date(); if (pr==null || pr.getUsed() || currenttime.after(pr.getExpireson()) ) { StatusMsg state = new StatusMsg(); state.settype("danger"); state.setmsg("Password Reset Faild! Please check your url."); states.add(state); mv.addObject("stat",states); mv.setViewName("showinfo"); } else { mv.addObject("token",token); mv.setViewName("setpass"); } } return mv; }
Example 10
Source File: RememberAuthenticationFilter.java From tianti with Apache License 2.0 | 5 votes |
/** * 判断是否让用户登陆 */ @Override protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { Subject subject = getSubject(request, response); return subject.isAuthenticated() || subject.isRemembered(); }
Example 11
Source File: UserAuthenticationFilter.java From belling-admin with Apache License 2.0 | 5 votes |
@Override protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { HttpServletRequest req = (HttpServletRequest) request; System.out.println("验证登陆信息==" + req.getRequestURI()); System.out.println("sessionId==" + req.getSession().getId()); Subject currentUser = SecurityUtils.getSubject(); if (null != currentUser) { if (currentUser.isAuthenticated() || currentUser.isRemembered()) { return true; } } // 返回false表示不执行后续的过滤器 return false; }
Example 12
Source File: UserController.java From songjhh_blog with Apache License 2.0 | 5 votes |
@RequestMapping(value = "/logout",method = RequestMethod.GET) public String logout() { Subject subject = SecurityUtils.getSubject(); Session session = subject.getSession(); if (subject.isAuthenticated()) { System.out.println(session.getLastAccessTime()); subject.logout(); } else if(subject.isRemembered()) { subject.logout(); } return "redirect:/"; }
Example 13
Source File: RememberAuthenticationFilter.java From mumu with Apache License 2.0 | 5 votes |
@Override protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { // 是登录页面 则直接返回true if (isLoginRequest(request, response)) { return true; } else { // 获取主题 Subject subject = getSubject(request, response); // 如果用户【未认证】 但是用户使用【记住我】功能 if (!subject.isAuthenticated() && subject.isRemembered()) { // 获取rememberMe的用户名称 Object principal = subject.getPrincipal(); if (null != principal) { //从session中 获取登录用户信息 //Session session = subject.getSession(true); //User sessionUser = (User) session.getAttribute(User.SESSION_USER); //如果session不保存用户信息;则从数据库中获取 //if (sessionUser == null) { // sessionUser = userService.getUserByName(String.valueOf(principal)); // subject.getSession().setAttribute(User.SESSION_USER, sessionUser); //} return true; } } } return true; }
Example 14
Source File: UserController.java From MyBlog with Apache License 2.0 | 5 votes |
@PostMapping("isLogin") @ResponseBody public MyResponse isLogin() { Subject subject = SecurityUtils.getSubject(); if (subject.isAuthenticated() || subject.isRemembered()) { return MyResponse.createResponse(ResponseEnum.ALREADY_LOGIN, SecurityUtils.getSubject().getPrincipal().toString()); } return MyResponse.createResponse(ResponseEnum.SUCC); }
Example 15
Source File: ShiroJwtVerifyingFilter.java From cassandra-reaper with Apache License 2.0 | 5 votes |
static Subject getJwtSubject(Subject nonJwt, ServletRequest req, ServletResponse res) { return null != nonJwt.getPrincipal() && (nonJwt.isRemembered() || nonJwt.isAuthenticated()) ? nonJwt : new WebSubject.Builder(req, res) .principals(new SimplePrincipalCollection(getJwtUser(req).get(), "jwtRealm")) .buildSubject(); }
Example 16
Source File: ErrorResponse.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
public ErrorResponse(final Throwable cause) { this(checkNotNull(cause).getMessage() == null ? cause.getClass().getName() : cause.getMessage()); authenticationRequired = cause instanceof UnauthenticatedException; if (authenticationRequired) { Subject subject = SecurityUtils.getSubject(); if (subject == null || !(subject.isRemembered() || subject.isAuthenticated())) { message = "Access denied (authentication required)"; } } }
Example 17
Source File: ForceLogoutFilter.java From jsets-shiro-spring-boot-starter with Apache License 2.0 | 5 votes |
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { Subject subject = getSubject(request, response); if (!subject.isAuthenticated() && !subject.isRemembered()) { return this.respondLogin(request, response); } Session currentSession = subject.getSession(); if (null!=currentSession.getAttribute(ShiroProperties.ATTRIBUTE_SESSION_FORCE_LOGOUT)) { String account = (String) subject.getPrincipal(); subject.logout(); this.authListenerManager.onForceLogout(request, account); return this.respondRedirect(request, response,this.properties.getForceLogoutUrl()); } return true; }
Example 18
Source File: RememberAuthenticationInterceptor.java From OneBlog with GNU General Public License v3.0 | 5 votes |
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Subject subject = SecurityUtils.getSubject(); if (subject.isAuthenticated()) { return true; } Session session = subject.getSession(true); if (session.getAttribute(SessionConst.USER_SESSION_KEY) != null) { return true; } if(!subject.isRemembered()) { log.warn("未设置“记住我”,跳转到登录页..."); response.sendRedirect(request.getContextPath() + "/passport/login"); return false; } try { Long userId = Long.parseLong(subject.getPrincipal().toString()); User user = userService.getByPrimaryKey(userId); UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), PasswordUtil.decrypt(user.getPassword(), user.getUsername()), true); subject.login(token); session.setAttribute(SessionConst.USER_SESSION_KEY, user); log.info("[{}] - 已自动登录", user.getUsername()); } catch (Exception e) { log.error("自动登录失败", e); response.sendRedirect(request.getContextPath() + "/passport/login"); return false; } return true; }
Example 19
Source File: MyLoginFilter.java From easyweb-shiro with MIT License | 5 votes |
@Override protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception { Subject subject = getSubject(servletRequest, servletResponse); if (!subject.isAuthenticated() && !subject.isRemembered()) { if (isAjax((HttpServletRequest) servletRequest)) { servletResponse.setContentType("application/json;charset=UTF-8"); PrintWriter out = servletResponse.getWriter(); out.write("{\"msg\":\"登录过期,请重新登录\",\"code\":401}"); out.flush(); return false; } } return true; }
Example 20
Source File: LoginBean.java From init-spring with Apache License 2.0 | 4 votes |
@RequestMapping(value = "/login", method = RequestMethod.POST) public String login(Model model, HttpServletRequest request) { Subject subject=SecurityUtils.getSubject(); if(subject.isAuthenticated()||subject.isRemembered()){ return "redirect:/home"; } String username = request.getParameter("username"); String password = request.getParameter("password"); String loginKaptchaCode = request.getParameter("code"); Session shiroSession = subject.getSession(); Object kaptchaCode = shiroSession.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); if (kaptchaCode!=null && !StringUtils.equalsIgnoreCase(loginKaptchaCode, kaptchaCode.toString())) { model.addAttribute("message", "验证码错误!"); return "/login"; } UsernamePasswordToken token = new UsernamePasswordToken(username, password, false, request.getRemoteHost()); try { subject.login(token); User user = jpaRealmRepository.findUserByName(username); user.setLastLogin(new Date()); user = jpaRealmRepository.mergeUser(user); return "redirect:/home"; } catch (UnknownAccountException uae) { model.addAttribute("message", "Unknown User!"); log.info("Unknown User!"); } catch (IncorrectCredentialsException ice) { model.addAttribute("message", "Incorrect Password!"); log.info("Incorrect Password!"); } catch (LockedAccountException lae) { model.addAttribute("message", "User Locked!"); log.info("User Locked!"); } catch (AuthenticationException ae) { model.addAttribute("message", "Authentication Failed!"); log.info("Authentication Failed!"); } return "/login"; }