• 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);
        }
    
    }

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

  • 相关阅读:
    (数据科学学习手札109)Python+Dash快速web应用开发——静态部件篇(中)
    (数据科学学习手札107)在Python中利用funct实现链式风格编程
    (数据科学学习手札106)Python+Dash快速web应用开发——回调交互篇(下)
    妙啊,速来get这9个jupyter实用技巧
    (数据科学学习手札105)Python+Dash快速web应用开发——回调交互篇(中)
    (在模仿中精进数据可视化07)星球研究所大坝分布可视化
    (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)
    基于 abp vNext 的快速开发模板
    JMeter四种参数化方式
    Python设计模式知多少
  • 原文地址:https://www.cnblogs.com/c1024/p/11011998.html
Copyright © 2020-2023  润新知