本文整理汇总了Java中org.apache.shiro.util.Factory类的典型用法代码示例。如果您正苦于以下问题:Java Factory类的具体用法?Java Factory怎么用?Java Factory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Factory类属于org.apache.shiro.util包,在下文中一共展示了Factory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setShiroConfiguration
import org.apache.shiro.util.Factory; //导入依赖的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);
}
}
示例2: loadShiroConfiguration
import org.apache.shiro.util.Factory; //导入依赖的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);
}
}
示例3: CentralDogmaSecurityManager
import org.apache.shiro.util.Factory; //导入依赖的package包/类
public CentralDogmaSecurityManager(File dataDir, Ini securityConfig) {
try {
sessionDao = new FileBasedSessionDAO(new File(dataDir, "_sessions").toPath());
} catch (IOException e) {
throw new IOError(e);
}
sessionManager = new CentralDogmaSessionManager(sessionDao);
final Factory<SecurityManager> factory = new IniSecurityManagerFactory(securityConfig) {
@Override
protected SecurityManager createDefaultInstance() {
DefaultSecurityManager securityManager = new DefaultSecurityManager();
securityManager.setSessionManager(sessionManager);
securityManager.setCacheManager(new MemoryConstrainedCacheManager());
return securityManager;
}
};
delegate = factory.getInstance();
}
示例4: testHelloWorld
import org.apache.shiro.util.Factory; //导入依赖的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();
}
示例5: testIniRealm
import org.apache.shiro.util.Factory; //导入依赖的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());
}
示例6: testCustomRealm
import org.apache.shiro.util.Factory; //导入依赖的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());
}
示例7: testIniAuthorization
import org.apache.shiro.util.Factory; //导入依赖的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);
}
示例8: testCustomRealmAuthorization
import org.apache.shiro.util.Factory; //导入依赖的package包/类
/**
* testCustomRealmAuthorization
* @Description: 使用自定义realm完成授权
* @return: void
* @Author: BeautifulSoup
* @Date: 2017年12月16日 下午3:05:46
*/
@Test
public void testCustomRealmAuthorization(){
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());
boolean isPermitted=subject.isPermittedAll("item:query");
System.out.println(isPermitted);
}
示例9: main
import org.apache.shiro.util.Factory; //导入依赖的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("认证失败");
}
}
示例10: testHelloworld
import org.apache.shiro.util.Factory; //导入依赖的package包/类
@Test
public void testHelloworld(){
//1.获取SecurityManagerFactory,此处用shiro.ini来初始化
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//2.得到securityManager实例
SecurityManager securityManager = factory.getInstance();
//3.绑定给securityManager
SecurityUtils.setSecurityManager(securityManager);
//4.获取subject
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
try {
//5.登录
subject.login(token);
} catch (Exception e) {
//6.身份验证失败
}
Assert.assertEquals(true, subject.isAuthenticated());
//7.登出
subject.logout();
}
示例11: testCustomRealm
import org.apache.shiro.util.Factory; //导入依赖的package包/类
@Test
public void testCustomRealm(){
//1.获取SecurityManagerFactory,此处用shiro-realm.ini来初始化(使用自定义realm)
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini");
//2.得到securityManager实例
SecurityManager securityManager = factory.getInstance();
//3.绑定给securityManager
SecurityUtils.setSecurityManager(securityManager);
//4.获取subject
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
try {
//5.登录
subject.login(token);
} catch (Exception e) {
//6.身份验证失败
}
Assert.assertEquals(true, subject.isAuthenticated());
//7.登出
subject.logout();
}
示例12: testCustomMultiRealm
import org.apache.shiro.util.Factory; //导入依赖的package包/类
@Test
public void testCustomMultiRealm(){
//1.获取SecurityManagerFactory,此处用shiro.ini来初始化(使用自定义realm)
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-multi-realm.ini");
//2.得到securityManager实例
SecurityManager securityManager = factory.getInstance();
//3.绑定给securityManager
SecurityUtils.setSecurityManager(securityManager);
//4.获取subject
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("wang", "123");
try {
//5.登录
subject.login(token);
} catch (Exception e) {
//6.身份验证失败
}
Assert.assertEquals(true, subject.isAuthenticated());
//7.登出
subject.logout();
}
示例13: testJDBCRealm
import org.apache.shiro.util.Factory; //导入依赖的package包/类
@Test
public void testJDBCRealm(){
//1.获取SecurityManagerFactory,此处用shiro-jdbc-realm.ini来初始化(使用自定义realm)
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-jdbc-realm.ini");
//2.得到securityManager实例
SecurityManager securityManager = factory.getInstance();
//3.绑定给securityManager
SecurityUtils.setSecurityManager(securityManager);
//4.获取subject
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
try {
//5.登录
subject.login(token);
} catch (Exception e) {
//6.身份验证失败
}
Assert.assertEquals(true, subject.isAuthenticated());
//7.登出
subject.logout();
}
示例14: main
import org.apache.shiro.util.Factory; //导入依赖的package包/类
public static void main(String args[]){
//读取配置文件,初始化工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini") ;
//获取SecurityManager的实例对象
SecurityManager instance = factory.getInstance();
SecurityUtils.setSecurityManager(instance);
//得到当前的用户
Subject curentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("crossoverjie","12341") ;
try {
curentUser.login(token);
System.out.println("认证成功");
}catch (Exception e){
e.printStackTrace();
System.out.println("认证失败");
}
curentUser.logout();
}
示例15: helloWorld
import org.apache.shiro.util.Factory; //导入依赖的package包/类
@Test
public void helloWorld() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager manager = factory.getInstance();
SecurityUtils.setSecurityManager(manager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
try {
subject.login(token);
} catch (AuthenticationException e) {
LOG.error("Authentication Invalid: " + e.getMessage());
}
Assert.assertEquals(true, subject.isAuthenticated());
subject.logout();
}