<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springboot-shiro</artifactId>
<version>0.1.0</version>
<properties>
<java.version>11</java.version>
<spring.boot.version>2.5.4</spring.boot.version>
<shiro.version>1.7.1</shiro.version>
</properties>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- Shiro Starter -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-starter</artifactId>
<version>${shiro.version}</version>
</dependency>
<!-- Other Dependencies -->
<!-- Add other dependencies here if needed -->
</dependencies>
<build>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Spring Boot Maven Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
</project>
import sun.net.www.protocol.http.AuthenticationInfo;
public class AccoutRealm extends AuthorizingRealm {
@Autowired
private AccountService accountService;
/**
* 授权
*
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo
doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
/**
* 认证
*
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo
doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
Account account = accountService.findByUsername(token.getUsername());
if (account != null) {
return new SimpleAuthenticationInfo(account, account.getPassword(), getName());
}
return null;
}
}
@Configuration
public class ShiroConfig {
@Bean
public ShiroFilterFactoryBean
shiroFilterFactoryBean(@Qualifier("securityManager")
DefaultWebSecurityManager securityManager){
ShiroFilterFactoryBean factoryBean = new
ShiroFilterFactoryBean();
factoryBean.setSecurityManager(securityManager);
return factoryBean;
}
@Bean
public DefaultWebSecurityManager
securityManager(@Qualifier("accoutRealm") AccoutRealm
accoutRealm){
DefaultWebSecurityManager manager = new
DefaultWebSecurityManager();
manager.setRealm(accoutRealm);
return manager;
}
@Bean
public AccoutRealm accoutRealm(){
return new AccoutRealm();
}
}
<dependency><groupId> com.github.theborakompanioni </groupId><artifactId> thymeleaf-extras-shiro </artifactId><version> 2.0.0 </version></dependency>
@Beanpublic ShiroDialect shiroDialect (){return new ShiroDialect ();}
<!DOCTYPE html><html lang = "en" xmlns:th = "http://www.thymeleaf.org"xmlns:shiro = "http://www.thymeleaf.org/thymeleaf-extrasshiro" ><head><meta charset = "UTF-8" ><title> Title </title><link rel = "shortcut icon" href = "#" /></head><body><h1> index </h1><div th:if = "${session.account != null}" ><span th:text = "${session.account.username}+' 欢迎回来! '" ></span><a href = "/logout" > 退出 </a></div><a href = "/main" > main </a> <br/><div shiro:hasPermission = "manage" ><a href = "manage" > manage </a> <br/></div><div shiro:hasRole = "administrator" ><a href = "/administrator" > administrator </a></div></body> </html>
文章来源地址https://www.toymoban.com/news/detail-701355.html
文章来源:https://www.toymoban.com/news/detail-701355.html
到了这里,关于Spring Boot 整合 Shiro(后端)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!