SpringBoot整合Security权限控制登录首页

目录
  • 在 pom 文件中增加thymeleaf页面支持
  • application.yml 配置文件
  • login 页面
  • controller目录下跳转配置
    • UserController

在 pom 文件中增加thymeleaf页面支持

<!-- 引入页面模板 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.yml 配置文件

创建 resources 目录文件夹目录为: src/main/resources 并将其设置为 resource 资源目录, 在resources目录下创建 application.yml 配置文件

spring:
  thymeleaf:
    cache: false
    check-template: true
    check-template-location: true
    content-type: text/html
    enabled: true
    encoding: UTF-8
    mode: HTML5
    prefix: classpath:/templates/
    suffix: .html

在resources目录下创建 templates 文件目录, 并在该目录下创建 index.html 和 login.html 页面文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>SpringBoot Security Integration</title>
</head>
<body>
</body>
</html>

login 页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>登录页面</title>
</head>
<body>
<form action="" method="post">
    <table>
        <tr>
            <td>用户名:</td>
            <td><input name="username" id="username" value=""/></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input name="password" id="password" value=""/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="submit" /></td>
        </tr>
    </table>
</form>
</body>
</html>

controller目录下跳转配置

在 java 源码目录下创建controller目录, 并在该目录下创建 HomeController/UserController 进行页面跳转配置

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * HomeController
 * 描述 : HomeController
 * 作者 : qianmoQ
 * 版本 : 1.0
 * 创建时间 : 2018-03-20 下午2:24
 */
@Controller
public class HomeController {
    /**
     * 首页
     *
     * @return 首页页面跳转
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    String home() {
        return "index";
    }
}

UserController

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * UserController
 * 描述 : UserController
 * 作者 : qianmoQ
 * 版本 : 1.0
 * 创建时间 : 2018-03-20 下午2:24
 */
@Controller
@RequestMapping(value = "user")
public class UserController {
    /**
     * 用户登录
     *
     * @return 用户登录页面跳转
     */
    @RequestMapping(value = "login", method = RequestMethod.GET)
    String login() {
        return "login";
    }
    /**
     * 用户注销退出
     *
     * @return 用户注销退出页面跳转
     */
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    String logout() {
        return "login";
    }
}

以上就是SpringBoot整合Security权限控制登录首页的详细内容,更多关于SpringBoot整合Security登录的资料请关注我们其它相关文章!

(0)

相关推荐

  • SpringBoot如何整合Springsecurity实现数据库登录及权限控制

    目录 第一步 第二步是封装一个自定义的类 第三步, 我们需要判断密码啦 总结 我们今天使用SpringBoot来整合SpringSecurity,来吧,不多BB 首先呢,是一个SpringBoot 项目,连接数据库,这里我使用的是mybaties.mysql, 下面是数据库的表 DROP TABLE IF EXISTS `xy_role`; CREATE TABLE `xy_role` ( `xyr_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键i

  • SpringBoot整合Security安全框架实现控制权限

    目录 一.前言 介绍: 官网: 优缺点: 案例: 二.环境准备 2.1.数据库表 2.2.导入依赖 2.3.配置文件 2.4.WebSecurityConfig Security的主要配置类: 2.5.Security身份验证 2.6.Security授权 2.7.UserDetailsService 2.7.MacLoginUrlAuthenticationEntryPoint 2.8.MyAccessDeniedHandler 2.9.MyLogoutSuccessHandler 2.10.

  • SpringBoot整合SpringSecurityOauth2实现鉴权动态权限问题

    目录 写在前面 准备 效果展示 实现 写在前面 思考:为什么需要鉴权呢? 系统开发好上线后,API接口会暴露在互联网上会存在一定的安全风险,例如:爬虫.恶意访问等.因此,我们需要对非开放API接口进行用户鉴权,鉴权通过之后再允许调用. 准备 spring-boot:2.1.4.RELEASE spring-security-oauth2:2.3.3.RELEASE(如果要使用源码,不要随意改动这个版本号,因为2.4往上的写法不一样了) mysql:5.7 效果展示 这边只用了postman做测试

  • SpringBoot 整合Security权限控制的初步配置

    正文 在源码目录下新建 config 目录, 在该目录下新建 WebSecurityConfig 类文件 /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding co

  • SpringBoot2.7 WebSecurityConfigurerAdapter类过期配置

    目录 前言 WebSecurityConfigurerAdapter 的注释 配置Spring Security 引入Web和Spring Security依赖 重写configure(WebSecurity方法进行配置 定制登录页面参数等 前言 进入到 SpringBoot2.7 时代,有小伙伴发现有一个常用的类忽然过期了: 在 Spring Security 时代,这个类可太重要了.过期的类当然可以继续使用,但是你要是决定别扭,只需要稍微看一下注释,基本上就明白该怎么玩了. WebSecur

  • SpringBoot整合Security权限控制登录首页

    目录 在 pom 文件中增加thymeleaf页面支持 application.yml 配置文件 login 页面 controller目录下跳转配置 UserController 在 pom 文件中增加thymeleaf页面支持 <!-- 引入页面模板 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thym

  • SpringBoot整合Security实现权限控制框架(案例详解)

    目录 一.前言 二.环境准备 2.1.数据库表 四.测试 五.总结 我想每个写项目的人,都肯定会遇到控制权限这个问题. 例如这个这个链接只能管理员访问,那个链接丫只能超级管理员访问等等,实现方式也有多种多样,控制的粒度也不一样. 以前刚学的时候,不会框架,大都是手写注解+过滤器来进行权限的控制,但这样增加了过滤器的负担.用起来也会稍微有些麻烦,粒度不太好控制. 用框架的话,就是封装了更多的操作,让一切更简单吧.当然不局限于Security,还有像Shiro安全框架,这两种非常常见. 一起加油吧!

  • springboot整合security和vue的实践

    目录 环境 1.security参考资料 认证流程原理: 2.springboot整合security要点 2.1获取登录用户信息 2.2自定义登入登出url 2.3自定义Handler返回json 2.4记住我功能 2.5验证码功能 2.6限制登录次数 2.7密码加密 2.8后台提供接口,返回前端json,整合vue做前端登入登出 3.测试 环境 springboot1.5.9 完整代码,内有sql,先建库,在运行sql建表,sql中已插入测试的数据. https://github.com/2

  • SpringBoot整合Keycloak实现单点登录的示例代码

    目录 1. 搭建Keycloak服务器 2. 配置权限 2.1. 登陆 2.2. 创建Realm 2.3. 创建用户 2.4. 创建客户端 2.5. 创建角色 2.6. 配置用户角色关系 2.7. 配置客户端和角色关系 3. 整合SpringBoot 3.1. 引入核心依赖 3.2. 编写Controller 3.3. 编写application.yml 4. 验证 Keycloak是一个开源的身份和权限访问管理工具,轻松为应用程序和安全服务添加身份验证,无需处理储存用户或者验证用户,其提供用户

  • java中自定义Spring Security权限控制管理示例(实战篇)

    背景描述 项目中需要做细粒的权限控制,细微至url + httpmethod (满足restful,例如: https://.../xxx/users/1, 某些角色只能查看(HTTP GET), 而无权进行增改删(POST, PUT, DELETE)). 表设计 为避嫌,只列出要用到的关键字段,其余敬请自行脑补. 1.admin_user 管理员用户表, 关键字段( id, role_id ). 2.t_role 角色表, 关键字段( id, privilege_id ). 3.t_privi

  • springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)

    1. 首先新建一个shiroConfig shiro的配置类,代码如下: @Configuration public class SpringShiroConfig { /** * @param realms 这儿使用接口集合是为了实现多验证登录时使用的 * @return */ @Bean public SecurityManager securityManager(Collection<Realm> realms) { DefaultWebSecurityManager sManager

  • SpringBoot整合SpringSession实现分布式登录详情

    目录 Session 共享 为什么服务器 A 登录后,请求发到服务器 B,不认识该用户? 解决方案 SpringBoot整合SpringSession实现分布式登录 Session 共享 比如两个域名: aaa.yupi.com bbb.yupi.com 如果要共享 cookie,可以种一个更高层的公共域名,比如 yupi.com 为什么服务器 A 登录后,请求发到服务器 B,不认识该用户? 用户在 A 登录,所以 session(用户登录信息)存在了 A 上 结果请求 B 时,B 没有用户信息

  • Spring MVC整合Shiro权限控制的方法

    Apache Shiro 是一个功能强大且灵活的开放源代码安全框架,可以细粒度地处理认证 (Authentication),授权 (Authorization),会话 (Session) 管理和加密 (cryptography) 等企业级应用中常见的安全控制流程. Apache Shiro 的首要目标是易于使用和理解. 有时候安全性的流程控制会非常复杂,对开发人员来说是件很头疼的事情,但并不一定如此. 框架就应该尽可能地掩盖复杂性,并公开一个简洁而直观的 API,从而简化开发人员的工作,确保其应

随机推荐