当前位置:网站首页>Shrio study notes (I)
Shrio study notes (I)
2022-04-22 08:06:00 【I forgot my nickname】
【 Personal understanding 】
Certification is done on top of authorization , When the user logs in , First verify its role , This is empowerment ; After successful authorization , Verify the login account and password , This is authentication .
A user can have multiple roles .
shiro By apache Provided . Create a maven project , add to shiro rely on
<!--shiro Core packages -->
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
One 、 authentication
shiro authentication
Code implementation : ad locum , adopt SimpleAccountRealm To simulate the account field , Save the user . Next , Step by step :
① establish SecurityManager
② The main body (subject) Request authentication
③ from SecurityManager Submit certification
④Authenticator authentication
⑤Realm verification
here , The focus is on the first three steps
private DefaultSecurityManager defaultSecurityManager;
// Simple account field , To demonstrate
private SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();//
@Before
public void addUser(){
// First add a user
simpleAccountRealm.addAccount("admin","123456");
}
@Test
public void testAuthentication(){
//1. structure SecurityManager Environmental Science
defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(simpleAccountRealm); // take simpleAccountRealm Set to manager Environment
//2. The authentication request is submitted by the subject
SecurityUtils.setSecurityManager(defaultSecurityManager); // Set up manager Environmental Science
Subject subject = SecurityUtils.getSubject(); // Get the subject
// Default constructor , Build the test data to verify
UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
subject.login(token ); // Submit authentication login
//3.1 Login authentication
System.out.println(" Certification results :"+subject.isAuthenticated());
//3.2 sign out
subject.logout();
System.out.println(" after , Certification results :"+subject.isAuthenticated());
}
Running effect :

Two 、 to grant authorization
shiro to grant authorization
The steps are similar to authentication , Relative to certification , Is to authorize first , Will be certified , In the code , Grant user permissions in the account domain , At the time of certification , Add authorization authentication , Other codes are similar to .
@Before
public void addUser(){
// First add a user , With role ( Multiple )
simpleAccountRealm.addAccount("admin","123456","admin","user");
}
Login authentication
subject.checkRoles("admin","user"); // to grant authorization
System.out.println(" Certification results :"+subject.isAuthenticated());

GOOD LUCK!!!
版权声明
本文为[I forgot my nickname]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220628165904.html
边栏推荐
猜你喜欢

How to connect Kunlun on state │ G series screen with Siemens 300 domestic MPI adapter

utgard连接opcserver报错Caused by: org.jinterop.dcom.common.JIRuntimeException: Access is denied. [0x800

js-继承

Configuration list of raspberry pie (entry reference)

Practical application of callable combined with countdownlatch

【Mock数据】fastmock根据传入参数动态返回mock内容

MySql查询指定一行排序到第一行

世平信息上榜《2021年中国网络安全市场全景图》

Charles使用之修改请求和响应的三种方式

web自动化:5.1selenium鼠标操作-单击、双击、右击、悬停、拖拽
随机推荐
Autoware demo test
世平信息上榜《CCSIP 2021中国网络安全产业全景图》
Web automation: 8.1 how to send JS instructions in the browser
web自动化:8.1如何在浏览器发送js指令
通用测试技术【二】测试方法
【Mock数据】fastmock根据传入参数动态返回mock内容
Shrio 学习笔记(一)
世平信息参与衡阳市“船山论坛”企业路演,畅谈数据安全,助力协同创新
mysql安装采坑
.net WebAPI访问授权机制及流程设计(header token+redis)
postman初级-6-console:打印并查看日志
助力2021杭州市网络安全宣传周 | 世平信息精彩活动合集
CefSharp存储Cookie和读Cookie
专注数据安全,世平信息上榜中国网络安全行业全景图六大细分领域
Shiping information was listed in the panorama of China's network security market in 2021
职场小白,频繁问问题会不会被嫌弃?
Web problem location: F12, how to find the corresponding interface
.net core CefSharp ChromiumWebBrowser中网站JS调用winfrom程序中C#方法
XSS range clearance
Shiping information has successfully passed the evaluation of "capability verification of data security products" of China Academy of communications and communications