您的位置:首页 > 编程语言 > Java开发

springboot的WEB开发笔记(SpringSecurity)

2020-08-10 20:10 1071 查看

SpringSecurity

  • 权限认证
  • SpringSecurity

    SpringSecurity官方使用文档:https://docs.spring.io/spring-security/site/docs/5.2.6.RELEASE/reference/htmlsingle/#jc

    依赖添加

    SpringSecurity配置的版本会受springboot的版本影响

    thymeleaf-extras-springsecurity5是适用于2.0.9.RELEASE以上的

    thymeleaf-extras-springsecurity4是适用于2.0.9.RELEASE以下的

    <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <scope>test</scope>
    </dependency>
    
    <dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
    </dependency>

    页面

    页面适用thymeleaf-extras-springsecurity的配置

    //thymeleaf-extras-springsecurity4的页面配置
    <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
    //thymeleaf-extras-springsecurity5的页面配置
    <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

    sec的运用

    //sec:authorize="!isAuthenticated()"进行一个是否登陆认证的验证,登陆认证则不显示
    <div sec:authorize="!isAuthenticated()"><a th:href="@{/toLogin}">登陆</a></div>
    //获取登陆用户的用户名
    <span sec:authentication="name">
    //获取登陆用户的权限,principal.authorities不可写成principal.getAuthorities()
    <span sec:authentication="principal.authorities">
    //根据用户权限判断是否显示
    <div sec:authorize="hasRole('vip1')"></div>

    授权

    ①进行WebSecurityConfigurerAdapter的扩展实现

    ②可以用http进行许多功能的配置

    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    /**
    * 授权
    * @param http
    * @throws Exception
    */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    //请求授权的规则:首页都可以访问,其他页面需要对应权限
    http.authorizeRequests()
    .antMatchers("/").permitAll()
    .antMatchers("/level1/**").hasRole("vip1")
    .antMatchers("/level2/**").hasRole("vip2");
    //没有权限就会到权限登陆页面
    http.formLogin();
    //注销
    http.logout().logoutSuccessUrl("/");
    //开启记住我
    http.rememberMe();
    }
    }

    授权的登陆页面为自定页面的操作有两种

    ①直接修改SecurityConfig类和页面表单提交的action,但这种方法具有缺点就是name=username/password需固定

    http.formLogin().loginPage("/toLogin");
    <form th:action="@{/toLogin}" method="post">

    ②loginPage为没有授权就会返回的登陆页面,loginProcessingUrl为处理页面的接口,usernameParameter,passwordParameter是网页传入的name值需要相对应

    http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("password").loginProcessingUrl("/login");

    授权的自定义rememberMe

    http.rememberMe().rememberMeParameter("remember");
    <input type="checkbox" name="remember">remember Me

    权限认证

    springboot 2.1.X 可以直接使用

    2.2.X以上的需要进行password的加密,以防反编译程序进行一个密码获取

    /**
    * 认证
    * @param auth
    * @throws Exception
    */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
    .withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
    .and()
    .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2");
    }
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: