11.2SpringSecurity整合thymeleaf

1.导包

SpringSecurity以及整合包

springsecurity5

<!--thymeleaf整合SpringSecurity-->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>
<!--security-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.编写AOP横切思想的SpringSecurity

@EnableWebSecurity开启

extends WebSecurityConfigurerAdapter集成自

编写授权和认证


//Aop横切
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    DataSource dataSource;
    //链式编程
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //登录页面和首页都可以访问,设置支出页面需要权限
        //请求授权的规则
        http.authorizeHttpRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/selPanyMoney.do").permitAll()
                .antMatchers("/index").hasRole("user")
                .antMatchers("/zhichu").hasRole("kuaiji");
        //初始默认会报错,跳到登录页面
        http.formLogin().loginPage("/tologin").usernameParameter("uname").passwordParameter("password").loginProcessingUrl("/login");
        //注销
        //Springboot默认开启防止网络攻击post加密或者关闭
        http.csrf().disable();
        http.logout().logoutSuccessUrl("/");
        //开启记住我
        //默认两周
        //自定义
        http.rememberMe().rememberMeParameter("remenmber");

    }

    //认证
    //密码需要加密编码
    //在SpringSecurity5.0+新增了很多加密方式
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //应该从数据库读取
        //这个是仿写的数据
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("sheep").password(new BCryptPasswordEncoder().encode("5519")).roles("user","kuaiji")
                .and()
                .withUser("user").password(new BCryptPasswordEncoder().encode("5519")).roles("user");
    }
}

http.formLogin().loginPage("/tologin").usernameParameter("uname").passwordParameter("password").loginProcessingUrl("/login");

与前端对应

loginPage("/tologin")指向我们登录界面如果没有那么就是默认的没有样式非常难看

usernameParameter("uname").passwordParameter("password")提交账号密码与角色去进行验证

loginProcessingUrl("/login")自带的验证方式不是自己编写的类

3.绑定前端

1.引入xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"

<form class="m-t" role="form" method="post" th:action="@{/login}">
    <div class="form-group">
        <input type="text" class="form-control" th:placeholder="#{login.username}" name="uname" required="required">
    </div>
    <div class="form-group">
        <input type="password" class="form-control"  th:placeholder="#{login.password}" name="password" required="required">
    </div>
    <p class="text-muted text-center">
    <input type="checkbox" name="remember">记住我
    </p>
</form>    

以下都必须对应版本好新版本必须5

判断角色权限如果有则给与显示 sec:authorize="hasRole('kuaiji')"

取出当前登录的用户名字 sec:authentication="name"

注意事项:

1. http.csrf().disable();关闭否则自己写的AJAX请求会403

2.springsecurity5版本才可以

3.账号密码一定要对应好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值