zoukankan      html  css  js  c++  java
  • [Java Spring] Implementing Spring Security

    pom.xml:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>

    SecurityConfiguration.java:

    package com.frankmoley.boot.essentials.initialbootapp;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.domain.ExampleMatcher;
    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;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.password.NoOpPasswordEncoder;
    
    @Configuration
    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(final HttpSecurity http) throws Exception {
    
            http.authorizeRequests().antMatchers("/", "/api")
                    // when it comes to / or /api/*, no need to check
                    .permitAll()
                    // any other reuqest should have authentication
                    .anyRequest().authenticated()
                .and()
    // if not authenticated, redirect to login form .formLogin()
    // allow /login for form login .loginPage("/login") .permitAll() .and() // allow logout .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { // not production code auth.inMemoryAuthentication() // just demo // in real world, use BCryptPasswordEncoder .passwordEncoder(NoOpPasswordEncoder.getInstance()) .withUser("user").password("password").roles("USER"); } }
  • 相关阅读:
    Mysql学习(慕课学习笔记7)修改数据表(下)
    Mysql学习(慕课学习笔记1)启动、登录及常用命令
    Mysql学习(慕课学习笔记2)数据库的创建与删除
    手机测试体系讲解
    Android开发之旅:环境搭建
    免费搭建wordpress博客有感
    第一篇
    浅谈通信网络(二)——信号
    小dai浅谈通信网络(一)——引子
    投票调查系统数据库设计及大家指教
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14068544.html
Copyright © 2011-2022 走看看