1简介 :
Shiro是用JAVA 写轻量级权限认证安全框架 ; 拥有对 session 封装缓存,记住 cookie
用户安全密码加密认证,权限校验等特性。
2 我对源码的见解:
PS : 流程概述 据我了解核心是FilterChain (过滤器链)
因为没有合适的DEMO 所以我用
也就是shiro 源码自带的DEMO 和自带的DEMO 代码演示一遍流程,虽然生产环境一般不会直接硬编码写死配置,但也会很好的帮助我们理解源码的执行流程。
先看下他的类
分别代码如下
@Configuration
@SpringBootApplication
public class CliApp { //NOPMD
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(CliApp.class, args);
// Grab the 'QuickStart' bean, call 'run()' to start the example.
context.getBean(QuickStart.class).run();//快速执行
}
/**
* Example hard coded Realm bean.
* @return hard coded Realm bean
*/
@Bean
public Realm realm() {
、、//自定义realm(域)一般都是自己定义,她这里也是自己定义
TextConfigurationRealm realm = new TextConfigurationRealm();
realm.setUserDefinitions("joe.coder=password,user\n" +
"jill.coder=password,admin");
realm.setRoleDefinitions("admin=read,write\n" +
"user=read");
realm.setCachingEnabled(true);
return realm;
}
@Component
public class QuickStart {
private static Logger log = LoggerFactory.getLogger(QuickStart.class);
@Autowired
private SecurityManager securityManager;
@Autowired
private SimpleService simpleService;
public void run() {
// get the current subject
Subject subject = SecurityUtils.getSubject();//subject 相当于每一个请求的主体 ,无论是机器还是请求对象,或是移动端请求对象,一般抽象成认证个体即可
// Subject is not authenticated yet
Assert.isTrue(!subject.isAuthenticated());//这里硬编码,一般配置都是过滤器也就是配置authenicationFilter就可以解决具体代码可以自己去看
// login the subject with a username / password
UsernamePasswordToken token = new UsernamePasswordToken("joe.coder", "password");//这里是封装用户名密码的TOKEN ,
subject.login(token); 、、//这里是核心代码配置过滤器也是核心代码拉!!!!!
// joe.coder has the "user" role
subject.checkRole("user");
// joe.coder does NOT have the admin role
Assert.isTrue(!subject.hasRole("admin")); //跟着这个实例走都能看懂
// joe.coder has the "read" permission
subject.checkPermission("read");//这里检查用户权限底层代码是去调了SimpleAccount的权限VAL