/** 获取用户Details信息的回调函数. */
  public UserDetails loadUserByUsername(String email)
      throws UsernameNotFoundException, DataAccessException {

    SystemUser user = userService.getUser(email);
    if (user == null) {
      throw new UsernameNotFoundException("用户" + email + " 不存在");
    }

    Set<GrantedAuthority> grantedAuths = obtainGrantedAuthorities(user);
    if (this.userService.isAdmin(user)) grantedAuths.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
    else grantedAuths.add(new GrantedAuthorityImpl("ROLE_USER"));

    if (user.getEmail().equals(Constants.SUPERADMIN)) {
      grantedAuths.add(new GrantedAuthorityImpl("ROLE_SUPERADMIN"));
    }

    // -- calkts示例中无以下属性, 暂时全部设为true. --//
    boolean enabled = true;
    boolean accountNonExpired = true;
    boolean credentialsNonExpired = true;
    boolean accountNonLocked = true;

    UserDetails userdetails =
        new org.springframework.security.core.userdetails.User(
            user.getEmail(),
            user.getPassword(),
            enabled,
            accountNonExpired,
            credentialsNonExpired,
            accountNonLocked,
            grantedAuths);

    return userdetails;
  }
  /**
   * 获得用户所有角色的权限集合.格式为R_roleID_TreeNodeID_OperationID,U_userId_TreeNodeID_OperationID
   * 调用springSecurityService
   */
  private Set<GrantedAuthority> obtainGrantedAuthorities(SystemUser user) {

    Session session = null;
    Connection connection = null;

    Set<TripleDTO> rolePrivilegeTriples = null;
    Set<TripleDTO> userPrivilegeTriples = null;
    Set<TripleDTO> adminPrivilegeTriples = null;
    try {
      session = sessionFactory.openSession();
      connection = session.connection();
      Set<Long> directRoles = springSecurityService.listDirectRoleNodes(user.getId(), connection);
      Set<Long> allRoles = springSecurityService.listParentNodes(directRoles, connection);
      userPrivilegeTriples = springSecurityService.getUserTriples(user.getId(), connection);
      rolePrivilegeTriples = springSecurityService.getRoleTriples(null, null, allRoles, connection);
      if (userService.isAdmin(user)) {
        Set<Long> directAdminNodes =
            springSecurityService.listDirectAdminNodes(user.getId(), connection);
        Set<Long> allAdminNodes = springSecurityService.listSubNodes(directAdminNodes, connection);
        adminPrivilegeTriples =
            springSecurityService.getAdminTriples(
                null, null, user.getId(), allAdminNodes, connection);
      }

    } catch (Exception e) {
      try {
        connection.rollback();
      } catch (SQLException e1) {

        e1.printStackTrace();
      }
      e.printStackTrace();

    } finally {
      if (session != null) session.close();
      try {
        if (connection != null) connection.close();
      } catch (SQLException e1) {

        e1.printStackTrace();
      }
    }

    // 格式化

    if (adminPrivilegeTriples == null) {
      return new AuthorityStringMaker(userPrivilegeTriples, rolePrivilegeTriples).makeAuthority();
    }

    return new AuthorityStringMaker(
            userPrivilegeTriples, rolePrivilegeTriples, adminPrivilegeTriples)
        .makeAuthority();
  }
Example #3
0
 private SystemUser getUser() {
   SecurityContext securityContext = SecurityContextHolder.getContext();
   String email = securityContext.getAuthentication().getName();
   SystemUser user = userService.getUser(email);
   return user;
 }
Example #4
0
  public void processPage(HTTP page, String parentUrl) {
    String url = page.getURL();
    if (rule.isNewsPage(url) && !rule.isNewsPage(parentUrl)) {
      System.out.println("url==============================" + url);
      if (!rule.isExist(page.getURL())) {
        Date time;
        Date publicDate;
        String title, orignkeyword, body, fromSource, summary, publicTime, newstype;
        body = page.getBody();

        time =
            Calendar.getInstance()
                .getTime(); // DateFormatUtils.format(Calendar.getInstance().getTime(),"yyyy-MM-dd
        // HH:mm:ss");

        // filetodisk.processFile(page);
        parser.setSource(body);

        title = parser.getTitle().trim();
        orignkeyword = parser.getKeyWord();
        fromSource = parser.getFromSource().trim();
        NetHelper nethelp = new NetHelper();
        System.out.println("摘要=" + parser.getSummary());
        System.out.println("url=" + url);
        System.out.println("标题=" + title);

        summary = nethelp.formatHtmlSource(parser.getSummary(), url);
        publicTime = parser.getPublicTime();
        publicDate = parser.getPublicDate();
        //				newstype = parser.getNewsType();
        Knowledge k = null;
        try {
          k = (Knowledge) Class.forName("edu.zju.cims201.GOF.hibernate.pojo.News").newInstance();
        } catch (InstantiationException e) {
          e.printStackTrace();
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
        }

        if (orignkeyword != null && !orignkeyword.equals("")) {
          orignkeyword = orignkeyword.replaceAll(",", ",");
          orignkeyword = orignkeyword.replaceAll("  ", ",");
          orignkeyword = orignkeyword.replaceAll("、", ",");
          orignkeyword = orignkeyword.replaceAll(";", ",");
          orignkeyword = orignkeyword.replaceAll(";", ",");
          String[] keywords = orignkeyword.split(",");
          Set<Keyword> keywordlist = new HashSet<Keyword>();
          for (String keyword : keywords) {
            if (null != keyword && !keyword.equals("")) {
              Keyword keywordT = kservice.SearchAndSaveKeyword(keyword.trim());
              keywordlist.add(keywordT);
            }
          }
          k.setKeywords(keywordlist);
        }
        Knowledgetype knowledgetype = kservice.SearchAndSaveKnowledgetype("新闻");
        k.setKnowledgetype(knowledgetype);
        List<Author> authorlist = new ArrayList<Author>();
        Author authorT = kservice.searchAndSaveAuthor(fromSource);
        authorlist.add(authorT);
        k.setKauthors(authorlist);
        SystemUser u = userservice.getUser(new Long(1));
        k.setUploader(u);
        k.setTitlename(title);
        Date uploadtimeT = new Date();
        k.setUploadtime(uploadtimeT);
        k.setIsvisible(true);
        Ktype ktype = ktypeservice.getKtype(new Long(9));
        k.setKtype(ktype);
        k.setStatus("0");
        k.setSecuritylevel("非密");

        Version version = new Version();
        version.setVersionTime(uploadtimeT);
        version.setVersionNumber("1.0");
        k.setVersion(version);
        k.setStatus("1");

        CommentRecord cr = new CommentRecord();
        cr.setCommentCount(new Long(0));
        cr.setViewCount(new Long(0));
        cr.setRate(new Float(0));
        cr.setDownloadCount(new Long(0));
        k.setCommentrecord(cr);

        try {
          PropertyUtils.setProperty(k, "newsbody", body);
          PropertyUtils.setProperty(k, "url", url);
          PropertyUtils.setProperty(k, "parenturl", parentUrl);
          PropertyUtils.setProperty(k, "newscontent", summary);
          PropertyUtils.setProperty(k, "publictime", publicTime);
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (InvocationTargetException e) {
          e.printStackTrace();
        } catch (NoSuchMethodException e) {
          e.printStackTrace();
        }

        //				News news = new News(title, keyword, time, body, url, parentUrl, summary, publicTime,
        // fromSource, publicDate, newstype);

        //				System.out.println("parentUrl = " + parentUrl);
        //				System.out.println("url = " + url);
        //				System.out.println("time = " + time);
        //				System.out.println("title = " + title);
        //				System.out.println("keyword = " + keyword);
        //				System.out.println("from = " + fromSource);
        //				System.out.println("publicTime = " + publicTime);
        //				System.out.println("summary = " + summary);
        // System.out.println("body = " + body);
        kservice.save(k, null);

      } else if (!rule.isContain(url, parentUrl)) {
        // 如果已经存在这个URL,即这个新闻已经存在,但是这个新闻的parenturl不是这个,那么把这个parentUrl也加进去。
        Knowledge k;
        try {
          k = (Knowledge) Class.forName("edu.zju.cims201.GOF.hibernate.pojo.News").newInstance();
        } catch (InstantiationException e) {
          e.printStackTrace();
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
        }
        k = rule.findNewsByUrl(url);
        String oldparenturl;
        try {
          oldparenturl = (String) PropertyUtils.getProperty(k, "parenturl");
          PropertyUtils.setProperty(k, "parenturl", oldparenturl + "|" + parentUrl);
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (InvocationTargetException e) {
          e.printStackTrace();
        } catch (NoSuchMethodException e) {
          e.printStackTrace();
        }
        kservice.save(k, null);
        //				News news = (News) newsdao.findByUrl(url).get(0);
        //				news.setParentUrl(news.getParentUrl() + "|" + parentUrl);
        //				newsdao.save(news);
      }
    }
  }