apache shiro jar包_只需要6个步骤,springboot集成shiro,并完成登录
时间: 2023-08-11 17:41:41 浏览: 177
好的,我可以为您提供关于Apache Shiro和Spring Boot集成的指导。以下是六个步骤:
1. 添加 Maven 依赖
您需要在 Maven 项目中添加以下依赖:
```
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-starter</artifactId>
<version>1.7.1</version>
</dependency>
```
2. 配置 Shiro
您需要在应用程序的 `application.properties` 文件中设置 Shiro 的属性。以下是一个示例:
```
# Shiro Configuration
shiro.loginUrl = /login
shiro.successUrl = /home
shiro.unauthorizedUrl = /unauthorized
shiro.filterChainDefinitionMap = /login=anon\n/home=authc\n/logout=logout
shiro.rememberMe.cookie.name = rememberMe
shiro.rememberMe.cookie.maxAge = 2592000
shiro.rememberMe.cipherKey = 3AvVhmFLUs0KTA3Kprsdag==
```
这里我们设置了登录页面,成功页面,未授权页面,过滤器链定义和记住我 cookie。
3. 创建 ShiroRealm
您需要创建一个继承 `AuthorizingRealm` 的类来实现自己的 Realm。以下是一个示例:
```
@Component
public class ShiroRealm extends AuthorizingRealm {
@Autowired
private UserService userService;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
User user = (User) principals.getPrimaryPrincipal();
authorizationInfo.addRole(user.getRole());
authorizationInfo.setStringPermissions(user.getPermissions());
return authorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
String email = usernamePasswordToken.getUsername();
User user = userService.findByEmail(email);
if (user == null) {
throw new UnknownAccountException();
}
return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
}
}
```
这里我们实现了 `doGetAuthorizationInfo()` 和 `doGetAuthenticationInfo()` 方法来实现授权和认证。
4. 配置 ShiroFilter
您需要在应用程序中创建一个 `ShiroFilter` 类来配置 Shiro 过滤器。以下是一个示例:
```
@Configuration
public class ShiroConfig {
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/logout", "logout");
filterChainDefinitionMap.put("/css/**", "anon");
filterChainDefinitionMap.put("/js/**", "anon");
filterChainDefinitionMap.put("/images/**", "anon");
filterChainDefinitionMap.put("/fonts/**", "anon");
filterChainDefinitionMap.put("/login", "anon");
filterChainDefinitionMap.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
shiroFilterFactoryBean.setLoginUrl("/login");
shiroFilterFactoryBean.setSuccessUrl("/home");
shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
return shiroFilterFactoryBean;
}
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(shiroRealm());
securityManager.setRememberMeManager(rememberMeManager());
return securityManager;
}
@Bean
public ShiroRealm shiroRealm() {
return new ShiroRealm();
}
@Bean
public RememberMeManager rememberMeManager() {
CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
rememberMeManager.setCookie(rememberMeCookie());
rememberMeManager.setCipherKey(Base64.decode("3AvVhmFLUs0KTA3Kprsdag=="));
return rememberMeManager;
}
@Bean
public SimpleCookie rememberMeCookie() {
SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
simpleCookie.setMaxAge(2592000);
return simpleCookie;
}
}
```
这里我们创建了一个 `ShiroFilterFactoryBean` 和一个 `SecurityManager`。我们还设置了过滤器链和记住我 cookie。
5. 创建登录页面
您需要创建一个登录页面来允许用户输入他们的凭证。以下是一个示例:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post" action="/login">
<label>Email:</label>
<input type="email" name="email">
<br>
<label>Password:</label>
<input type="password" name="password">
<br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
6. 创建授权页面
您需要创建一个授权页面来显示用户是否已被授权。以下是一个示例:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>Welcome!</h1>
<p>You have been successfully authenticated and authorized.</p>
</body>
</html>
```
以上是关于Apache Shiro和Spring Boot集成的六个步骤。希望这可以帮助你!
阅读全文