• springsecurity《三》


    接着二讲:之前是安装其源码的原理,在配置类里面,设置角色啊,路径权相匹配什么的,
    那么问题来了,那么多用户我不可能手动在配置类一个个hasAnyRole,antMatchers。。。逗号分隔配置吧,想想Spring要一统江湖,也不会那么蠢,肯定有便捷的东西,比如注解
    
         //角色必以ROLE开头
    //    @Secured("ROLE_tom")   大小写眼科区分角色必以ROLE开头
        // //角色必以ROLE开头与否皆可
       //@PreAuthorize("hasRole('tom')")  访问资源之前校验
    //    @PostAuthorize("hasRole('tom')")  访问资源之后校验
        @PostMapping("/show")
        public String show() {
            return "redirect:show.html";
        }
    
    果然有,在某个资源上使用注解,设置权限,@PreAuthorize("hasRole('tom')")全等于@Secured("ROLE_tom"),如果角色和AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,ROLE_tom,/show.html"));里面的相符合
    

     

    就可以进行访问
    
     https://www.cnblogs.com/wangbiaohistory/p/16245785.html 里面说了,有各种访问角色,匿名,管理员,认证,记住我这几种授权登录方式,我们来试试登陆后的记住我功能
    
    @Autowired
    private PersistentTokenRepository persistentTokenRepository;
    配置类里面:
       //记住我
            http.rememberMe()
                    .userDetailsService(userDetailsService)
                    .tokenRepository(persistentTokenRepository);
    
    
    /**
     * springsecurity数据库配置
     */
    @Configurable
    @Component
    public class ConfigSecurity {
    
        @Autowired
        private DataSource dataSource;
    
        @Bean
        public PersistentTokenRepository gePersistentTokenRepository(){
            JdbcTokenRepositoryImpl jdbcTokenRepository=new JdbcTokenRepositoryImpl();
            jdbcTokenRepository.setDataSource(dataSource);
            //第一次启动要去建表,这里打开,后面再次启动就要关掉,不然会报错
            jdbcTokenRepository.setCreateTableOnStartup(true);
            return  jdbcTokenRepository;
        }
    }
    
    启动后就会在数据库创建一张表,用于记录最后一次登陆人的标记:
    
    
    CREATE TABLE `persistent_logins` (
      `username` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
      `series` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
      `token` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
      `last_used` timestamp NOT NULL,
      PRIMARY KEY (`series`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='username   用户名\r\nseries          主键\r\ntoken          token\r\nlast_used    最后一次登陆时间\r\n';
    
    有人说你怎么知道会自动创建表:jdbcTokenRepository.setCreateTableOnStartup(true);巧不巧,点进去看下哈哈哈,jdbc熟悉的身影有木有
    

     

    csrf跨站伪造开启
                    .and().csrf().disable(); 把这行关闭
    服务端有源码可知需要传递_csrf.token得值,每次访问必须携带才可以
    
    
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>测试springsecurity</title>
    </head>
    <body>
    
    <form action="/login" method="post">
        <input type="hidden" value="${_csrf.token}" name="_csrf">
      用户名:  <input type="text" name="laoliu123" />
      密码:  <input type="password" name="laoliu456" />
      记住我:  <input type="checkbox" name="remember-me" vaue="true"/>
        <input  type="submit" value="登录"/>
    </form>
    </body>
    </html>
    
  • 相关阅读:
    Ubuntu14.04安装ROS Indigo
    STM32F103移植uCOSIII始终卡在PendSV或Systick处解决办法
    STM32F103移植uCOSIII始终卡在PendSV或Systick处解决办法
    WIN7下PS/2等键盘失灵无法使用的解决办法--实测有效
    WIN7下PS/2等键盘失灵无法使用的解决办法--实测有效
    在altium designer9 等中使用protell99se的如0805,0603等PCB封装库
    在altium designer9 等中使用protell99se的如0805,0603等PCB封装库
    VB将输入文本框的数字分割并按十六进制发送
    Windows 10同步时间的方法
    maven安装cucumber的pom文件设置
  • 原文地址:https://www.cnblogs.com/wangbiaohistory/p/16251100.html
Copyright © 2020-2023  润新知