zoukankan      html  css  js  c++  java
  • SpringSecurity学习

    Springboot整合SpringSecurity

    导入security的依赖,关于security的配置

    @EnableWebSecurity
    public class MyConfig 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();
    //注销页面------logoutSuccessUrl是配置注销后去的页面,也可以配置注销后的清除cookies或者session,在源码中都有写
    http.logout().logoutSuccessUrl("/");
        }
    configure----http是授权,配置权限,设置登录页面和注销页面,注销页面可以看源码,请求是/logout,http的登录(POST的请求)要设置http.csrf.disease();
    因为security默认开启的跨域保护,要关闭掉,要不会报403没有权限的错误。
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
    .withUser("chao").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1")
    .and()
    .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
    }
    关于认证的配置,以上是在内存中设置用户.
    --------------------------------------------------------------------------
    因为每个用户的限权都不一样,所以登陆每个不同的页面,应该也都显示不同,用thymleaf和security的整合
    <dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>3.0.4.RELEASE</version>
    </dependency>
    index页面代码如下
    <!--如果已经登陆======显示 用户名,注销-->
    <div sec:authorize="isAuthenticated()">
    用户名:<span sec:authentication="name"></span>
    <a href="/logout">注销</a>
    </div>
    <!--如果未登录======显示 登录-->
    <div sec:authorize="!isAuthenticated()">

    <a href="/toLogin">登录</a>
    </div>

    导入以上依赖,使用sec:标签的话,springboot高版本不支持,把版本降到2.0.9------------设置好之后,在注销的时候又出现问题了,就是csrf保护,因为我是用a标签注销的,是不安全的,所以csrf会开启保护,
    得把这个关了,要不然注销显示404,或者用post方式注销

    可以用
    <div style="float: left" sec:authorize="hasRole('vip2')">来根据限权动态显示div
    ---------------------------------------------------------------------------------------------
    关于记住我 这个功能要去配置文件中
    http.rememberMe();开启,之后会保存在cookies中,可以去浏览器的application中查看
    ----------------------------------------------------------------------------------
    配置自定义登录页
    http.formLogin().loginPage("/toLogin");
    http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login");//设置默认登陆页面和登录url

    对应的login页面代码
    <form action="/login" method="post">
    用户名:<input type="text" name="username">
    密码:<input type="text" name="password">
    <input type="submit" value="登录">
    </form>
    看源码可以知道默认的参数名是username和password,可以用方法改,
    自己的登录页面的记住我配置如下:
    http.rememberMe().rememberMeParameter("remenber");
    ---------------------------------------------------------------------------
    认证连接数据库,配置中如下
    auth.userDetailsService(adminServiceimpl)
    .passwordEncoder(new BCryptPasswordEncoder());//password必须设置加密的格式
    bean类要
    class Admin implements UserDetails;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() { //重写这个方法,自己定义一个类实现GrantedAuthoritiy
    List<GrantedAuthority> auths = new LinkedList<>();
    auths.add(new SimpleGrantedAuthority(this.getGrade()));
    return auths;
    }

    service类要
    AdminServiceimpl implements UserDetailsService;
    @override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    Admin admin = adminMapper.selectByUsername(s);
    String passEnc = admin.getPassword();
    String encode = new BCryptPasswordEncoder().encode(passEnc);
    admin.setPassword(encode);
    return admin;
    一个重点:数据库设置权限等级时,级别的格式是ROLE_
    不加的话就不会起作用



     
     
  • 相关阅读:
    2017-9-8-Linux下VNC server开启&图形界面显示
    2017-9-8-RaspberryPi安装过程
    2017-9-7-Linux Mint TFTP服务安装开启
    2017-9-7-第一篇博客
    面试回答优缺点问题
    多层板的层叠和压合结构
    磁珠和电感
    关于TVS、ESD、稳压二极管、压敏电阻
    STM8硬件设计注意事项
    根据电路板画出电路原理图的方法
  • 原文地址:https://www.cnblogs.com/w123w/p/13683416.html
Copyright © 2011-2022 走看看