当前位置: 首页 > wzjs >正文

华云电力建设监理公司网站手机网站模板图片

华云电力建设监理公司网站,手机网站模板图片,网站建设的优缺点,网站图片缩略图在企业级应用开发中,权限管理和用户认证是非常重要的组成部分。Spring Boot 以其便捷的配置和快速的开发流程而受到开发者们的青睐。Apache Shiro 是一个强大且易用的 Java 安全框架,可以非常容易地开发出足够安全的应用。本文将介绍如何在 Spring Boot …

在企业级应用开发中,权限管理和用户认证是非常重要的组成部分。Spring Boot 以其便捷的配置和快速的开发流程而受到开发者们的青睐。Apache Shiro 是一个强大且易用的 Java 安全框架,可以非常容易地开发出足够安全的应用。本文将介绍如何在 Spring Boot 应用中集成 Apache Shiro 来实现权限管理。

一、环境准备

  1. 开发工具
  2. IntelliJ IDEA 或 Eclipse
  3. Maven
  4. Java 11+
  5. 项目初始化
  6. 使用 Spring Initializr 创建一个新的 Spring Boot 项目。
  7. 添加 Web 和 JPA 依赖。
  8. 添加 Shiro 依赖: 在 pom.xml 文件中添加 Shiro 的相关依赖。
xml

深色版本

1<dependencies>
2    <!-- Shiro -->
3    <dependency>
4        <groupId>org.apache.shiro</groupId>
5        <artifactId>shiro-spring</artifactId>
6        <version>1.12.4</version>
7    </dependency>
8    <dependency>
9        <groupId>org.apache.shiro</groupId>
10        <artifactId>shiro-ehcache</artifactId>
11        <version>1.12.4</version>
12    </dependency>
13    <!-- 其他依赖 -->
14</dependencies>

二、配置文件

  1. application.properties
  2. properties
  3. 深色版本
  4. 1# Shiro 配置 2shiro.cacheManager.ehCacheManager.shared=true
  5. ShiroConfig.java: 这里我们定义 Shiro 的配置类。
java

深色版本

1@Configuration
2public class ShiroConfig {
3
4    @Bean
5    public SecurityManager securityManager() {
6        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
7        securityManager.setRealm(myShiroRealm());
8        securityManager.setCacheManager(cacheManager());
9        return securityManager;
10    }
11
12    @Bean
13    public MyShiroRealm myShiroRealm() {
14        return new MyShiroRealm();
15    }
16
17    @Bean
18    public CacheManager cacheManager() {
19        return new EhCacheManager();
20    }
21
22    @Bean
23    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
24        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
25        factoryBean.setSecurityManager(securityManager);
26        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
27        // 设置匿名访问路径
28        filterChainDefinitionMap.put("/login", "anon");
29        // 设置需要认证的路径
30        filterChainDefinitionMap.put("/**", "authc");
31        factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
32        return factoryBean;
33    }
34}

三、自定义 Realm

  1. MyShiroRealm.java: 实现 AuthorizingRealm 接口来定义认证逻辑。
java

深色版本

1@Component
2public class MyShiroRealm extends AuthorizingRealm {
3
4    @Override
5    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
6        // 从 token 中获取用户名
7        String username = (String) token.getPrincipal();
8        // 查询数据库验证用户名和密码
9        if ("admin".equals(username) && "password".equals(token.getCredentials().toString())) {
10            return new SimpleAuthenticationInfo(username, "password", getName());
11        }
12        throw new UnknownAccountException();
13    }
14
15    @Override
16    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
17        String username = (String) principals.getPrimaryPrincipal();
18        // 查询数据库获取该用户的权限信息
19        if ("admin".equals(username)) {
20            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
21            info.addRole("admin");
22            info.addStringPermission("user:read");
23            return info;
24        }
25        return null;
26    }
27}

四、控制器与视图

  1. LoginController.java: 实现登录功能。
java

深色版本

1@Controller
2public class LoginController {
3
4    @GetMapping("/login")
5    public String login() {
6        return "login";
7    }
8
9    @PostMapping("/login")
10    public String login(@RequestParam String username, @RequestParam String password, Model model) {
11        try {
12            Subject currentUser = SecurityUtils.getSubject();
13            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
14            currentUser.login(token);
15            return "redirect:/dashboard";
16        } catch (UnknownAccountException e) {
17            model.addAttribute("error", "未知账户!");
18            return "login";
19        } catch (IncorrectCredentialsException e) {
20            model.addAttribute("error", "密码错误!");
21            return "login";
22        }
23    }
24}

五、总结

通过上述步骤,我们成功地在 Spring Boot 应用中集成了 Apache Shiro,并实现了基本的权限管理。当然,实际应用中还需要考虑更多的细节,比如更复杂的权限判断逻辑、异常处理、多数据源支持等。希望这篇博客能为你的项目提供一些有用的参考。


文章转载自:

http://lq50IMpQ.hfyLL.cn
http://HtyUio3K.hfyLL.cn
http://7nYsYDBU.hfyLL.cn
http://BZ7EIl97.hfyLL.cn
http://upMKATcE.hfyLL.cn
http://MRJicBBV.hfyLL.cn
http://iDQeIhns.hfyLL.cn
http://siB8Uuot.hfyLL.cn
http://a6HblhWp.hfyLL.cn
http://9A5dvFmy.hfyLL.cn
http://0dNvLATK.hfyLL.cn
http://MMjX2jIV.hfyLL.cn
http://GdtLJ2I9.hfyLL.cn
http://ckFxf7PQ.hfyLL.cn
http://rwwpW3GT.hfyLL.cn
http://ovkG81DI.hfyLL.cn
http://QJr7795j.hfyLL.cn
http://Q2NLpXIz.hfyLL.cn
http://gR0IBomp.hfyLL.cn
http://ePBhC1u2.hfyLL.cn
http://14Z9Qyjm.hfyLL.cn
http://vVYxxWwp.hfyLL.cn
http://e6VPr3zq.hfyLL.cn
http://KmhMmt1f.hfyLL.cn
http://ZiJzkb42.hfyLL.cn
http://1D9xv6Ku.hfyLL.cn
http://MSE66Ret.hfyLL.cn
http://jDbnaYMd.hfyLL.cn
http://t2LwMNcj.hfyLL.cn
http://ZQYWEWzx.hfyLL.cn
http://www.dtcms.com/wzjs/777151.html

相关文章:

  • 网站建设人员架构京津冀协同发展战略的主要内容
  • 广告公司做的网站字体侵权哈尔滨的网络科技开发公司
  • 陕西省建设银行分行互联网互联网站客户关系管理的内容
  • 怀来县住房和城乡规划建设局网站wordpress英文版变中文版
  • 12306网站是哪个公司做的上海高端建设网站
  • 网站忧化教程四川 优质高职建设网站
  • 泰语网站怎么建设lua做网站
  • 农产品网站建设及优化项目如何注册自己的工作室
  • 西安是哪个省的城市小说网站seo排名怎么做
  • 邯郸做紧固件网站网站首页qq在线咨询js
  • 做搜狗手机网站快速排我找伟宏篷布我做的事ko家的网站
  • 如何建设网站平台企业手机app开发公司
  • 南京林业大学实验与建设网站徐州个人建站模板
  • 随州网站制作价格北海哪里做网站
  • 电脑网站和手机网站怎么做相同路径ppt模板 网站开发
  • 设计名字的网站网站免费创建
  • 城建公司建设网站基础资料凡客诚品官网app
  • 织梦贷款网站源码网络安全公司排名前十名
  • 做门户网站那个系统好小微宝安网站建设
  • 双语网站开发深圳网络推广优化
  • 国内高清视频素材网站苏州集团网站设计企业
  • 做导购网站企业网站有哪些优点
  • 西安建网站的公司大庆建设集团网站
  • 商城网站设计图专业做网站优化排名
  • 怎样找到免费的黄页网站燕郊个人网站建设
  • 建设视频网站多少钱建立一个网站的前期资金
  • 为什么 要建设网站网站建设教育培训
  • 省建设干部培训中心网站网站手机端设计
  • 清远做网站的公司一线城市做网站工资有多少
  • 车工订单网站页面设计比例