【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)

前言

本篇博客主要是分享,使用SpringSecurity开发基于表单的认证(一)


一、SpringSecurity核心功能

【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第1张图片

  • 认证(你是谁)
    1.实现用户名+密码
    2.实现手机号+短信认证
  • 授权(能干什么)
  • 攻击防护(防止伪造身份)
package com.zcw.security.browser;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.stereotype.Component;

/**
 * @ClassName : BrowserSecurityConfig
 * @Description :适配器类
 * @Author : Zhaocunwei
 * @Date: 2020-06-18 17:43
 */
@Component
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //表单登录
        http.formLogin()
                .and()
                //授权
                .authorizeRequests()
                .anyRequest()
                .authenticated();
    }
}


  • 启动项目:
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第2张图片
  • 测试:
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第3张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第4张图片

二、自定义用户认证逻辑

  • 处理用户信息获取逻辑

package com.zcw.security.browser;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @ClassName : MyUserDetailsService
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-06-18 18:31
 */
@Component
@Slf4j
public class MyUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("登录用户名:"+username);
        return new User(username,
                //在数据库中存的密码
                "123456",
                //数据库中权限
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}


  • 测试:
    密码随便输入:
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第5张图片
    在这里插入图片描述
    输入正确的密码:
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第6张图片
    在这里插入图片描述
  • 处理用户校验逻辑
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第7张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第8张图片
    测试:
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第9张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第10张图片
  • 处理密码加密解密
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第11张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第12张图片

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第13张图片

import org.springframework.security.crypto.password.PasswordEncoder;

【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第14张图片
测试:
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第15张图片
在这里插入图片描述
正确输入:123456,两次发现加盐验证:
在这里插入图片描述
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第16张图片

三、个性化用户认证流程

  • 自定义登录页面
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第17张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第18张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第19张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第20张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第21张图片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
    <h2>标准登录页面</h2>
    <h3>表单登录</h3>
    <form action="/authentication/form" method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2"><button type="submit">登录</button></td>
            </tr>
        </table>
    </form>
</body>
</html>

【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第22张图片
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第23张图片
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第24张图片

  • 自定义登录成功处理
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第25张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第26张图片
package com.zcw.security.browser.support;

import lombok.Data;

/**
 * @ClassName : SimpleResponse
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-06-19 13:41
 */
@Data
public class SimpleResponse {
    public SimpleResponse(Object content){
        this.content = content;
    }
    private Object content;
}


【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第27张图片
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第28张图片
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第29张图片
创建如图所示几个类:
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第30张图片

package com.zcw.security.core.properties;

import lombok.Data;

/**
 * @ClassName : BrowserProperties
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-06-19 13:55
 */
@Data
public class BrowserProperties {
    private String loginPage;
}



package com.zcw.security.core.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @ClassName : SecurityProperties
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-06-19 13:54
 */

@ConfigurationProperties(prefix = "zcw.security")
@Data
public class SecurityProperties {
    private BrowserProperties browserProperties = new BrowserProperties();
}


package com.zcw.security.core;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName : SecurityCoreConfig
 * @Description : 配置类
 * @Author : Zhaocunwei
 * @Date: 2020-06-19 13:59
 */
@Configuration
@EnableConfigurationProperties(SecurityCoreConfig.class)//使我们的配置类生效
public class SecurityCoreConfig {
}


【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第31张图片
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第32张图片

  • 配置默认数据,
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第33张图片
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第34张图片
  • 测试:

项目启动报错:

java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3068b369: startup date [Fri Jun 19 14:10:04 CST 2020]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6a47b187
	at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:427) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:999) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:958) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:750) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at com.zcw.DemoApplication.main(DemoApplication.java:20) [classes/:na]

2020-06-19 14:10:07.772 ERROR 18616 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3068b369: startup date [Fri Jun 19 14:10:04 CST 2020]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6a47b187
	at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:230) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1006) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:958) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:750) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at com.zcw.DemoApplication.main(DemoApplication.java:20) [classes/:na]

【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第35张图片

Not registered via @EnableConfigurationProperties or marked as Spring component 
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>

  • 感觉还是jar包冲突了,需要解决:
    【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第36张图片
    还是没解决,不是jar包的问题:
    进行打包时,出现错误,解决:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:repackage (default) on project zcw-securit
y-demo: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:repackage failed: Unable to find a sing
le main class from the following candidates [com.zcw.DemoApplication, com.zcw.wiremock.MockServer] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :zcw-security-demo


连续清了三遍:

mvn clean
 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第37张图片
【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第38张图片

【Spring Security技术栈开发企业级认证与授权】----使用Spring Security开发基于表单的登录(一)_第39张图片

你可能感兴趣的:(Spring,#,Spring,权限,表单,security,认证与授权)