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


Java SecurityUtils.setSecurityManager方法代碼示例

本文整理匯總了Java中org.apache.shiro.SecurityUtils.setSecurityManager方法的典型用法代碼示例。如果您正苦於以下問題:Java SecurityUtils.setSecurityManager方法的具體用法?Java SecurityUtils.setSecurityManager怎麽用?Java SecurityUtils.setSecurityManager使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.shiro.SecurityUtils的用法示例。


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

示例1: setShiroConfiguration

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
/**
 * Sets shiro configuration to the path of the resource
 * that points to the {@code shiro.ini} file.
 *
 * @param resource the resource
 */
@Autowired
public void setShiroConfiguration(@Value("${shiro.authn.config.file:classpath:shiro.ini}") final Resource resource) {
    try {
        if (resource.exists()) {
            final String location = resource.getURI().toString();
            logger.debug("Loading Shiro configuration from {}", location);

            final Factory<SecurityManager> factory = new IniSecurityManagerFactory(location);
            final SecurityManager securityManager = factory.getInstance();
            SecurityUtils.setSecurityManager(securityManager);
        } else {
            logger.debug("Shiro configuration is not defined");
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:24,代碼來源:ShiroAuthenticationHandler.java

示例2: loadShiroConfiguration

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
/**
 * Sets shiro configuration to the path of the resource
 * that points to the {@code shiro.ini} file.
 *
 * @param resource the resource
 */
public void loadShiroConfiguration(final Resource resource) {
    try {
        final Resource shiroResource = ResourceUtils.prepareClasspathResourceIfNeeded(resource);
        if (shiroResource != null && shiroResource.exists()) {
            final String location = shiroResource.getURI().toString();
            LOGGER.debug("Loading Shiro configuration from [{}]", location);

            final Factory<SecurityManager> factory = new IniSecurityManagerFactory(location);
            final SecurityManager securityManager = factory.getInstance();
            SecurityUtils.setSecurityManager(securityManager);
        } else {
            LOGGER.debug("Shiro configuration is not defined");
        }
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
}
 
開發者ID:mrluo735,項目名稱:cas-5.1.0,代碼行數:24,代碼來源:ShiroAuthenticationHandler.java

示例3: main

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
public static void main(String[] args) {
  //此處從ini文件來實現用用戶角色權限配置,實際多從數據庫表來實現
  Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini.bak");

  //SercurityManager 對象
  SecurityManager instance = factory.getInstance();
  SecurityUtils.setSecurityManager(instance);

  //測試用戶
  Subject currentUser = SecurityUtils.getSubject();
  UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");

  boolean result = false;
  try {
    currentUser.login(token);
    result = true;
    LOG.debug("認證成功");
  } catch (Exception e) {
    result = false;
    LOG.debug("認證失敗");
  }

}
 
開發者ID:MiniPa,項目名稱:cjs_ssms,代碼行數:24,代碼來源:RealmTest.java

示例4: testHelloWorld

import org.apache.shiro.SecurityUtils; //導入方法依賴的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

示例5: testIniRealm

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
/**
 * testIniRealm
 * @Description: iniRealm的測試
 * @return: void
 * @Author: BeautifulSoup
 * @Date: 2017年12月16日 上午11:41:43
 */
@Test
@Ignore
public void testIniRealm(){
	Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:inirealm-shiro.ini");
	SecurityManager securityManager = factory.getInstance();
	SecurityUtils.setSecurityManager(securityManager);
	Subject subject = SecurityUtils.getSubject();
	UsernamePasswordToken token=new UsernamePasswordToken("james_shu", "1997admin");
	try{
		subject.login(token);
	}catch(AuthenticationException e){
		e.printStackTrace();
	}
	System.out.println("用戶認證狀態:"+subject.isAuthenticated());
	subject.logout();
	System.out.println("用戶當前認證狀態:"+subject.isAuthenticated());
}
 
開發者ID:fuyunwang,項目名稱:ShiroDemo,代碼行數:25,代碼來源:AuthenticationTest.java

示例6: testCustomRealm

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
/**
 * testCustomRealm
 * @Description: CustomRealm的測試
 * @return: void
 * @Author: BeautifulSoup
 * @Date: 2017年12月16日 上午11:41:53
 */
@Test
public void testCustomRealm(){
	Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:customrealm-shiro.ini");
	SecurityManager securityManager = factory.getInstance();
	SecurityUtils.setSecurityManager(securityManager);
	Subject subject = SecurityUtils.getSubject();
	
	
	UsernamePasswordToken token=new UsernamePasswordToken("BeautifulSoup", "1997admin");
	try{
		subject.login(token);
	}catch(AuthenticationException e){
		e.printStackTrace();
	}
	System.out.println("用戶認證狀態:"+subject.isAuthenticated());
	subject.logout();
	System.out.println("用戶當前認證狀態:"+subject.isAuthenticated());
}
 
開發者ID:fuyunwang,項目名稱:ShiroDemo,代碼行數:26,代碼來源:AuthenticationTest.java

示例7: testIniAuthorization

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
/**
 * testIniAuthorization
 * @Description: 使用inirealm完成授權
 * @return: void
 * @Author: BeautifulSoup
 * @Date: 2017年12月16日 下午3:05:34
 */
@Test
@Ignore
public void testIniAuthorization(){
	Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:permission-shiro.ini");
	SecurityManager securityManager = factory.getInstance();
	SecurityUtils.setSecurityManager(securityManager);
	Subject subject = SecurityUtils.getSubject();
	//首先認證,認證通過之後才能授權
	UsernamePasswordToken token=new UsernamePasswordToken("beautifulsoup", "password");
	try{
		subject.login(token);
	}catch(AuthenticationException e){
		e.printStackTrace();
	}
	System.out.println("用戶的認證狀態:"+subject.isAuthenticated());
	boolean isPermitted=subject.isPermittedAll("user:create:01","user:query");
	subject.checkPermissions("user:create:01","user:query");
	System.out.println(isPermitted);
}
 
開發者ID:fuyunwang,項目名稱:ShiroDemo,代碼行數:27,代碼來源:AuthorizationTest.java

示例8: setUp

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
@BeforeEach
void setUp() {

  IniSecurityManagerFactory factory = new IniSecurityManagerFactory("./src/test/resources/shiro-test.ini");
  SecurityManager securityManager = factory.getInstance();
  SecurityUtils.setSecurityManager(securityManager);
  subject = SecurityUtils.getSubject();
}
 
開發者ID:Java-Publications,項目名稱:vaadin-016-helloworld-14,代碼行數:9,代碼來源:ShiroTest.java

示例9: logout

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
@RequestMapping(value = "/logout")
public String logout() {
	SecurityUtils.setSecurityManager(securityManager);
	Subject subject = SecurityUtils.getSubject();
	subject.logout();
	return "redirect:/l/tologin";
}
 
開發者ID:phoenix-varus,項目名稱:jeeWe,代碼行數:8,代碼來源:LoginController.java

示例10: setSecurityManager

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
public void setSecurityManager(SecurityManager securityManager) {
  if (securityManager == null) {
    return;
  }

  this.securityManager = securityManager;
  Realm realm = new CustomAuthRealm(securityManager);
  DefaultSecurityManager shiroManager = new DefaultSecurityManager(realm);
  SecurityUtils.setSecurityManager(shiroManager);
  increaseShiroGlobalSessionTimeout(shiroManager);

  isIntegratedSecurity = true;
  isClientAuthenticator = false;
  isPeerAuthenticator = false;
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:16,代碼來源:IntegratedSecurityService.java

示例11: main

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
public static void main(String[] args) {
    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini");
    SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);

    Subject subject = SecurityUtils.getSubject();

    UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","111111");

    try {
        subject.login(token);
    }catch (AuthenticationException e){
        e.printStackTrace();
    }
    // 是否認證通過
    boolean isAuthenticated = subject.isAuthenticated();

    System.out.println("是否認證通過:" + isAuthenticated);

    // 退出操作
    subject.logout();

    // 是否認證通過
    isAuthenticated = subject.isAuthenticated();

    System.out.println("是否認證通過:" + isAuthenticated);
}
 
開發者ID:lgpzjp,項目名稱:rure,代碼行數:28,代碼來源:ShiroTest.java

示例12: WebAuthConfiguration

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
public WebAuthConfiguration() {
    Factory<SecurityManager> factory = new IniSecurityManagerFactory(INI_LOCATION);
    SecurityManager securityManager=factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
}
 
開發者ID:WhatAKitty,項目名稱:spark-project,代碼行數:6,代碼來源:WebAuthConfiguration.java

示例13: test

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
@Test
public void test(){

    log.info("My First Apache Shiro Application");

    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
    SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);

    // get the currently executing user:
    Subject currentUser = SecurityUtils.getSubject();

    // Do some stuff with a Session (no need for a web or EJB container!!!)
    Session session = currentUser.getSession();
    session.setAttribute("someKey", "aValue");
    String value = (String) session.getAttribute("someKey");
    if (value.equals("aValue")) {
        log.info("Retrieved the correct value! [" + value + "]");
    }

    // let's login the current user so we can check against roles and permissions:
    if (!currentUser.isAuthenticated()) {
        UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
        token.setRememberMe(true);
        try {
            currentUser.login(token);
        } catch (UnknownAccountException uae) {
            log.info("There is no user with username of " + token.getPrincipal());
        } catch (IncorrectCredentialsException ice) {
            log.info("Password for account " + token.getPrincipal() + " was incorrect!");
        } catch (LockedAccountException lae) {
            log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                    "Please contact your administrator to unlock it.");
        }
        // ... catch more exceptions here (maybe custom ones specific to your application?
        catch (AuthenticationException ae) {
            //unexpected condition?  error?
        }
    }

    //say who they are:
    //print their identifying principal (in this case, a username):
    log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

    //test a role:
    if (currentUser.hasRole("schwartz")) {
        log.info("May the Schwartz be with you!");
    } else {
        log.info("Hello, mere mortal.");
    }

    //test a typed permission (not instance-level)
    if (currentUser.isPermitted("lightsaber:weild")) {
        log.info("You may use a lightsaber ring.  Use it wisely.");
    } else {
        log.info("Sorry, lightsaber rings are for schwartz masters only.");
    }

    //a (very powerful) Instance Level permission:
    if (currentUser.isPermitted("winnebago:drive:eagle5")) {
        log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                "Here are the keys - have fun!");
    } else {
        log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
    }
    //all done - log out!
    currentUser.logout();
}
 
開發者ID:followwwind,項目名稱:apache,代碼行數:69,代碼來源:ShiroTest.java

示例14: UserLoggingServlet

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
public UserLoggingServlet() {
	Factory<org.apache.shiro.mgt.SecurityManager> shiroFactory = new IniSecurityManagerFactory();
	org.apache.shiro.mgt.SecurityManager securityManager = shiroFactory.getInstance();
	SecurityUtils.setSecurityManager(securityManager);
}
 
開發者ID:ETspielberg,項目名稱:bibliometrics,代碼行數:6,代碼來源:UserLoggingServlet.java

示例15: login

import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping(value = "/login")
public ModelAndView login(String username, String password) {
	Map map = new HashMap<String, String>();
	ModelAndView mav = new ModelAndView();

	if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
		map.put("status", "faied");
		map.put("msg", "用戶名和密碼不能為空");
		mav.addObject("detail", map);
		return mav;
	}

	UsernamePasswordToken token = new UsernamePasswordToken(username, password);
	SecurityUtils.setSecurityManager(securityManager);
	Subject subject = SecurityUtils.getSubject();
	try {
		subject.login(token);
	} catch (IncorrectCredentialsException ice) {
		// 捕獲密碼錯誤異常
		map.put("status", "failed");
		map.put("msg", "密碼錯誤");
		mav.addObject("detail", map);
		return mav;
	} catch (UnknownAccountException uae) {
		// 捕獲未知用戶名異常
		map.put("status", "failed");
		map.put("msg", "用戶不存在");
		mav.addObject("detail", map);
		return mav;
	} catch (ExcessiveAttemptsException eae) {
		// 捕獲錯誤登錄過多的異常
		map.put("status", "failed");
		map.put("msg", "請稍後再試");
		mav.addObject("detail", map);
		return mav;
	}
	map.put("status", "success");
	map.put("msg", "登錄成功");
	mav.addObject("detail", map);
	return mav;
}
 
開發者ID:phoenix-varus,項目名稱:jeeWe,代碼行數:43,代碼來源:LoginController.java


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