本文整理汇总了Java中org.apache.shiro.mgt.SecurityManager类的典型用法代码示例。如果您正苦于以下问题:Java SecurityManager类的具体用法?Java SecurityManager怎么用?Java SecurityManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SecurityManager类属于org.apache.shiro.mgt包,在下文中一共展示了SecurityManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shiroFilter
import org.apache.shiro.mgt.SecurityManager; //导入依赖的package包/类
@Bean(name="shiroFilter")
public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager")SecurityManager manager) {
ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
bean.setSecurityManager(manager);
//配置登录的url和登录成功的url
bean.setLoginUrl("/login");
bean.setSuccessUrl("/home");
//配置访问权限
LinkedHashMap<String, String> filterChainDefinitionMap=new LinkedHashMap<>();
filterChainDefinitionMap.put("/login", "anon"); //表示可以匿名访问
filterChainDefinitionMap.put("/common/**", "anon");
filterChainDefinitionMap.put("/js/**","anon");
filterChainDefinitionMap.put("/druid/**","anon");
filterChainDefinitionMap.put("/captcha","anon");
filterChainDefinitionMap.put("/**", "authc");//表示需要认证才可以访问
bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return bean;
}
示例2: provideSecurityManager
import org.apache.shiro.mgt.SecurityManager; //导入依赖的package包/类
@Provides
@Singleton
SecurityManager provideSecurityManager(
SessionManager sessionManager,
RememberMeManager rememberMeManager,
Realms realms,
Set<AuthenticationListener> authListeners) {
DefaultSecurityManager manager = new DefaultSecurityManager(realms.getRealms());
((AbstractAuthenticator) manager.getAuthenticator()).setAuthenticationListeners(authListeners);
manager.setSessionManager(sessionManager);
manager.setRememberMeManager(rememberMeManager);
return manager;
}
示例3: setShiroConfiguration
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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);
}
}
示例4: createInstance
import org.apache.shiro.mgt.SecurityManager; //导入依赖的package包/类
@Override
protected AbstractShiroFilter createInstance() throws Exception {
SecurityManager securityManager = getSecurityManager();
if (securityManager == null){
throw new BeanInitializationException("SecurityManager property must be set.");
}
if (!(securityManager instanceof WebSecurityManager)){
throw new BeanInitializationException("The security manager does not implement the WebSecurityManager interface.");
}
PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
FilterChainManager chainManager = createFilterChainManager();
chainResolver.setFilterChainManager(chainManager);
return new MySpringShiroFilter((WebSecurityManager)securityManager, chainResolver);
}
示例5: loadShiroConfiguration
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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);
}
}
示例6: CentralDogmaSecurityManager
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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();
}
示例7: testHelloWorld
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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();
}
示例8: testIniRealm
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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());
}
示例9: testCustomRealm
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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());
}
示例10: testIniAuthorization
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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);
}
示例11: testCustomRealmAuthorization
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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);
}
示例12: shirFilter
import org.apache.shiro.mgt.SecurityManager; //导入依赖的package包/类
@Bean("shiroFilter")
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
shiroFilter.setSecurityManager(securityManager);
shiroFilter.setLoginUrl("/login.html");
//shiroFilter.setSuccessUrl("/index.html");
shiroFilter.setUnauthorizedUrl("/");
Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/public/**", "anon");
filterMap.put("/webjars/**", "anon");
filterMap.put("/api/**", "anon");
//swagger配置
filterMap.put("/swagger**", "anon");
filterMap.put("/v2/api-docs", "anon");
filterMap.put("/swagger-resources/configuration/ui", "anon");
filterMap.put("/login.html", "anon");
filterMap.put("/sys/login", "anon");
filterMap.put("/captcha.jpg", "anon");
filterMap.put("/**", "authc");
shiroFilter.setFilterChainDefinitionMap(filterMap);
return shiroFilter;
}
示例13: shiroFilter
import org.apache.shiro.mgt.SecurityManager; //导入依赖的package包/类
/**
* shiroFilter
* @param manager
* @return
*/
@Bean(name="shiroFilter")
public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manager) {
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
bean.setSecurityManager(manager);
// 配置登录的url
bean.setLoginUrl("/login");
//bean.setSuccessUrl("/admin/main");
// 过滤链
LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/login", "anon"); // 表示可以匿名访问
filterChainDefinitionMap.put("/admin/**", "authc");// 管理员权限需要验证过滤
bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return bean;
}
示例14: shiroFilter
import org.apache.shiro.mgt.SecurityManager; //导入依赖的package包/类
@Bean(name="shiroFilter")
public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manager) {
ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
bean.setSecurityManager(manager);
//配置登录的url和登录成功的url
bean.setLoginUrl("/login");
//配置访问权限
LinkedHashMap<String, String> filterChainDefinitionMap=new LinkedHashMap<>();
filterChainDefinitionMap.put("/login", "anon"); //表示可以匿名访问
filterChainDefinitionMap.put("/captcha","anon");
filterChainDefinitionMap.put("/common/**", "anon");
filterChainDefinitionMap.put("/default/**","anon");
filterChainDefinitionMap.put("/druid/**", "anon");
filterChainDefinitionMap.put("/**", "authc");//表示需要认证才可以访问
//filterChainDefinitionMap.put("/**", "anon");
bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return bean;
}
示例15: main
import org.apache.shiro.mgt.SecurityManager; //导入依赖的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("认证失败");
}
}