updated example codes for Apache shiro and Vavr Collections API
This commit is contained in:
@@ -2,11 +2,12 @@ package com.baeldung;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.*;
|
||||
import org.apache.shiro.config.IniSecurityManagerFactory;
|
||||
import org.apache.shiro.mgt.DefaultSecurityManager;
|
||||
import org.apache.shiro.mgt.SecurityManager;
|
||||
import org.apache.shiro.realm.Realm;
|
||||
import org.apache.shiro.realm.text.IniRealm;
|
||||
import org.apache.shiro.session.Session;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.Factory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -16,9 +17,8 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Factory<SecurityManager> factory
|
||||
= new IniSecurityManagerFactory("classpath:shiro.ini");
|
||||
SecurityManager securityManager = factory.getInstance();
|
||||
Realm realm = new MyCustomRealm();
|
||||
SecurityManager securityManager = new DefaultSecurityManager(realm);
|
||||
|
||||
SecurityUtils.setSecurityManager(securityManager);
|
||||
Subject currentUser = SecurityUtils.getSubject();
|
||||
@@ -28,7 +28,7 @@ public class Main {
|
||||
= new UsernamePasswordToken("user", "password");
|
||||
token.setRememberMe(true);
|
||||
try {
|
||||
currentUser.login(token);
|
||||
currentUser.login(token);
|
||||
} catch (UnknownAccountException uae) {
|
||||
log.error("Username Not Found!", uae);
|
||||
} catch (IncorrectCredentialsException ice) {
|
||||
@@ -43,7 +43,7 @@ public class Main {
|
||||
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
|
||||
|
||||
if (currentUser.hasRole("admin")) {
|
||||
log.info("Welcome Admin");
|
||||
log.info("Welcome Admin");
|
||||
} else if(currentUser.hasRole("editor")) {
|
||||
log.info("Welcome, Editor!");
|
||||
} else if(currentUser.hasRole("author")) {
|
||||
@@ -53,19 +53,19 @@ public class Main {
|
||||
}
|
||||
|
||||
if(currentUser.isPermitted("articles:compose")) {
|
||||
log.info("You can compose an article");
|
||||
log.info("You can compose an article");
|
||||
} else {
|
||||
log.info("You are not permitted to compose an article!");
|
||||
}
|
||||
|
||||
if(currentUser.isPermitted("articles:save")) {
|
||||
log.info("You can save articles");
|
||||
log.info("You can save articles");
|
||||
} else {
|
||||
log.info("You can not save articles");
|
||||
}
|
||||
|
||||
if(currentUser.isPermitted("articles:publish")) {
|
||||
log.info("You can publish articles");
|
||||
log.info("You can publish articles");
|
||||
} else {
|
||||
log.info("You can not publish articles");
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public class Main {
|
||||
session.setAttribute("key", "value");
|
||||
String value = (String) session.getAttribute("key");
|
||||
if (value.equals("value")) {
|
||||
log.info("Retrieved the correct value! [" + value + "]");
|
||||
log.info("Retrieved the correct value! [" + value + "]");
|
||||
}
|
||||
|
||||
currentUser.logout();
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
jdbcRealm = com.baeldung.MyCustomRealm
|
||||
[users]
|
||||
user = password,admin
|
||||
user2 = password2,editor
|
||||
user3 = password3,author
|
||||
|
||||
securityManager.realms = $jdbcRealm
|
||||
[roles]
|
||||
admin = *
|
||||
editor = articles:*
|
||||
author = articles:compose,articles:save
|
||||
Reference in New Issue
Block a user