zoukankan      html  css  js  c++  java
  • springboot07-security

    1.pom中添加thymeleaf和security依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!--测试-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!--mvc-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--thymeleaf模板-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <!--权限-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
    </dependencies>
    View Code

    2.配置WebSecurityConfig

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    
    /**
     * WebSecurityConfig类描述:
     *
     * @author yangzhenlong
     * @since 2017/2/22
     */
    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/", "/index").permitAll()
                    .anyRequest().authenticated()
                .and()
                    .formLogin().loginPage("/login").permitAll()
                .and()
                    .logout().permitAll();
        }
    
        /**
         * 在内存中创建了一个用户,该用户的名称为admin,密码为admin,用户角色为USER
         * @param builder
         * @throws Exception
         */
        @Autowired
        public void createUser(AuthenticationManagerBuilder builder) throws Exception {
            builder
                    .inMemoryAuthentication()
                    .withUser("admin")
                    .password("admin")
                    .roles("USER");
        }
    }
    View Code

    3.写自己的Controller

    @Controller
    public class MainController {
    
        @RequestMapping("/")
        public String index(){
            return "index";
        }
    
        @RequestMapping("/index")
        public String index2(){
            return "index";
        }
    
        @RequestMapping("/hello")
        public String hello(){
            return "hello";
        }
    
        @RequestMapping("/login")
        public String login(){
            return "login";
        }
    }
    View Code

    4.启动springboot,访问http://localhost:8080/

    如果没有登录,访问/hello时会自动跳转到/login

    用我们内存中创建的 admin/admin登录

    点击注销,跳转到/login

  • 相关阅读:
    【转】 url中文乱码问题
    [转]Jquery 点击图片在弹出层显示大图
    JQuery获取和设置Select选项的常用方法总结
    springMVC框架下返回json格式的对象,list,map
    sqlserver数据库 表中字段值有空格,如何去除空格(例如char (5) 存入数据不足5位时sqlserver会自动补空格)
    jquery Jbox 插件实现弹出窗口在修改的数据之后,关闭弹出窗口刷新父页面的问题
    sqlserver 2008 r2 直接下载地址,可用迅雷下载
    web服务器与tomcat
    xml入门与解析
    jdbc框架-dbutils的简单使用
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/6547051.html
Copyright © 2011-2022 走看看