zoukankan      html  css  js  c++  java
  • SpringBoot整合SpringSecurity简单案例

    在我们开发项目的过程中经常会用到一些权限管理框架,Java领域里边经常用的可能就是shiro了,与之对应的还有SpringSecurity,SpringSecurity可以说是非常强大,与Spring可以无缝整合,但是学习难度也高,今天给大家分享一个demo级别的。

    pom.xml加入以下依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    创建一个页面:home.html

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
        <head>
            <title>Spring Security Example</title>
        </head>
        <body>
            <h1>Welcome!</h1>
    
            <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
        </body>
    </html>

    hello.html

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
          xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
        <head>
            <title>Hello World!</title>
        </head>
        <body>
            <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
            <form th:action="@{/logout}" method="post">
                <input type="submit" value="Sign Out"/>
            </form>
        </body>
    </html>

    配置MVC端点:

    @Configuration
    public class MvcConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/home").setViewName("home");
            registry.addViewController("/").setViewName("home");
            registry.addViewController("/hello").setViewName("hello");
            registry.addViewController("/login").setViewName("login");
        }
    
    }

    SpringSecurity配置

    @Configuration
    @EnableWebSecurity 
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter { <1>
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http 
                .authorizeRequests()
                    .antMatchers("/", "/home").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll();
        }
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth 
                .inMemoryAuthentication()
                    .withUser("admin").password("admin").roles("USER");
        }
    }

    登录页面如下:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
          xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
        <head>
            <title>Spring Security Example </title>
        </head>
        <body>
            <div th:if="${param.error}">
                Invalid username and password.
            </div>
            <div th:if="${param.logout}">
                You have been logged out.
            </div>
            <form th:action="@{/login}" method="post">
                <div><label> User Name : <input type="text" name="username"/> </label></div>
                <div><label> Password: <input type="password" name="password"/> </label></div>
                <div><input type="submit" value="Sign In"/></div>
            </form>
        </body>
    </html>

    启动类:

    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) throws Throwable {
            SpringApplication.run(Application.class, args);
        }
    
    }

    有问题可以在下面评论,技术问题可以私聊我。

  • 相关阅读:
    Chamfer Distance--倒角距离
    javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    mysql单个索引和联合索引的区别
    鸽一下
    笔记:关于 INT1 INT0 中断说明记录 (2020-07-16)[85.22%]
    使用 Git 管理 KiCad EDA 项目文件 [2020-06-28][26.77%]
    从单片机基础到程序框架 2019版(2020-07-04)[12.66%]
    KiCad Pcbnew 中现代工具箱 (2020-06-24)[98.33%]
    【营养研究一】鸡蛋和牛奶的营养对比 (2020-06-23)[95.89%]
    git 忽略上传指定文件 命令
  • 原文地址:https://www.cnblogs.com/c1024/p/11011998.html
Copyright © 2011-2022 走看看