本文整理匯總了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);
}
}
示例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);
}
}
示例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("認證失敗");
}
}
示例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();
}
示例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());
}
示例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());
}
示例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);
}
示例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();
}
示例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";
}
示例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;
}
示例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);
}
示例12: WebAuthConfiguration
import org.apache.shiro.SecurityUtils; //導入方法依賴的package包/類
public WebAuthConfiguration() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory(INI_LOCATION);
SecurityManager securityManager=factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
}
示例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();
}
示例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);
}
示例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;
}