zoukankan      html  css  js  c++  java
  • spring security

    spring security

    依赖

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

    用户认证和授权

    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //所有人都能访问首页,有对应权限的人才能访问功能页
            //请求授权的规则
            http.authorizeRequests()
                    .antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasRole("vip1")
                    .antMatchers("/level2/**").hasRole("vip2")
                    .antMatchers("/level3/**").hasRole("vip3");
    
            //没有权限,默认回到登陆页面
            http.formLogin();
            //开启注销,回到指定页面
            http.logout().logoutSuccessUrl("/");
            //开启记住用户
            http.rememberMe().rememberMeParameter("remember");
        }
    
        //认证
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                    .withUser("abc").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2")
                    .and()
                    .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3");
        }
    }
    

    使用数据库认证

    @Autowired
    private DataSource dataSource;
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // ensure the passwords are encoded properly
        UserBuilder users = User.withDefaultPasswordEncoder();
        auth
            .jdbcAuthentication()
                .dataSource(dataSource)
                .withDefaultSchema()
                .withUser(users.username("user").password("password").roles("USER"))
                .withUser(users.username("admin").password("password").roles("USER","ADMIN"));
    }
    

    注销与权限控制

    依赖

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        <version>3.0.4.RELEASE</version>
    </dependency>
    

    首页

    <div class="right menu">
        <!--未登录-->
        <div sec:authorize="!isAuthenticated()">
            <a class="item" th:href="@{/toLogin}">
                <i class="address card icon"></i> 登录
            </a>
        </div>
    
        <!--已登录-->
        <div sec:authorize="isAuthenticated()">
            <a class="item">
                用户名: <span sec:authentication="name"></span>
                权限: <span sec:authentication="authorities"></span>
            </a>
        </div>
        <div sec:authorize="isAuthenticated()">
            <a class="item" th:href="@{/logout}">
                <i class="sign-out icon"></i> 注销
            </a>
        </div>
    </div>
    

    按权限展示

    sec:authorize="hasRole('vip1')"
    

    记住账户和首页定制

    登录页

    <input type="checkbox" name="remember"> 记住我
    
  • 相关阅读:
    matlab 修改窗口logo 使用Javaframe
    matlab guide 开发心得
    判断两个集合的包含关系
    xaml 中 引用嵌套类的对象
    xaml中显示 “大括号左边” 文本
    步进电机脉冲与毫米
    最短路径:Dijkstra算法 C#
    JPA自定义sql的三种方式
    double相乘少一分的问题,BigDecimal参数传小数也会出问题
    String类小知识
  • 原文地址:https://www.cnblogs.com/pinked/p/12368535.html
Copyright © 2011-2022 走看看