博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring security 入门(1)
阅读量:6150 次
发布时间:2019-06-21

本文共 5740 字,大约阅读时间需要 19 分钟。

hot3.png

最后的项目结构:

pom.xml

org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.mdtech.security
md-security
0.0.1-SNAPSHOT
md-security
spring security学习
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-security
org.springframework.security.oauth.boot
spring-security-oauth2-autoconfigure
org.projectlombok
lombok
true

UserController.ajava

package com.mdtech.security.demo.modules.user.controller;import com.mdtech.security.demo.modules.user.model.User;import com.mdtech.security.demo.modules.user.service.UserService;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author calebzhao<9 3 9 3 4 7 5 0 7 @ qq.com> * 2019/5/11 15:55 */@RestController@RequestMapping("/user")@Api(tags = "用户模块")public class UserController {    @Autowired    private UserService userService;    @ApiOperation("查询用户详情")    @GetMapping("/get")    public User getUserById(long id){        User user = userService.getUserById(id);        return user;    }}

BrowserSecurityConfig.java

package com.mdtech.security.borwser.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.crypto.password.NoOpPasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;/** * @author calebzhao * @date 2019/5/10 16:55 */@Configurationpublic class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {//        http.authorizeRequests()//                    .anyRequest().permitAll();        http                .formLogin()                    .loginPage("/login.html")                    .loginProcessingUrl("/user/login")                .and()                .authorizeRequests()                    .antMatchers("/login.html").permitAll()                    .anyRequest().authenticated()                .and()                .csrf().disable();    }    @Bean    public PasswordEncoder passwordEncoder(){        return NoOpPasswordEncoder.getInstance();    }}

AuthenticateUserDetailService.java

package com.mdtech.security.borwser.authentication;import com.mdtech.security.borwser.model.Address;import com.mdtech.security.borwser.model.UserModel;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;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Optional;/** * @author calebzhao<9 3 9 3 4 7 5 0 7 @ qq.com> * 2019/5/11 20:47 */@Componentpublic class AuthenticateUserDetailService implements UserDetailsService {//    @Autowired//    private PasswordEncoder passwordEncoder;    private List
userModelList = new ArrayList<>(); public AuthenticateUserDetailService(){ UserModel userModel = new UserModel(); userModel.setId(1L); userModel.setNickname("zhangsan"); userModel.setUsername("calebzhao"); userModel.setPassword("123"); userModel.setBirthday(new Date()); Address address1 = new Address(); address1.setId(1); address1.setProvinceId("111"); address1.setCityId("222"); address1.setDistrictId("333"); Address address2 = new Address(); address2.setId(2); address2.setProvinceId("1112"); address2.setCityId("2222"); address2.setDistrictId("3333"); List addressList = new ArrayList<>(2); addressList.add(address1); addressList.add(address2); userModel.setAddressList(addressList); userModelList.add(userModel); } @Override public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { Optional
userOptional = this.userModelList.stream().filter(item -> item.getUsername().equals(s)).findFirst(); if (!userOptional.isPresent()){ return null; } UserModel userModel = userOptional.get(); User user = new User(s, userModel.getPassword(), AuthorityUtils.createAuthorityList("admin")); return user; }}

登录页面 src\main\resources\resources\login.html

    
标准登录

登录

application.yml

server:  port: 7070spring:  profiles:    active: @spring.profiles.active@  application:    name: md-security-demo

访问localhosy:7070

转载于:https://my.oschina.net/zhaopeng2012/blog/3048544

你可能感兴趣的文章
java相关
查看>>
由一个异常开始思考springmvc参数解析
查看>>
向上扩展型SSD 将可满足向外扩展需求
查看>>
jenkins updatecenter更新插件有问题
查看>>
我的友情链接
查看>>
SQL Server -- T-SQL 编码标准
查看>>
图解Team Foundation Server 2013系列
查看>>
智能语音控制中心 - 树莓派、Nanopi、Orangepi语音识别控制
查看>>
利用SCVMM 2012 R2来管理Azure虚拟机
查看>>
在U盘上安装ESXi 4.1.0
查看>>
北信源内网安全管理管理系统
查看>>
邮件营销整体解决方案
查看>>
借助工具Profwiz进行加域及账户配置文件迁移
查看>>
09-OSPF故障排查总结
查看>>
ORACLE 10g 下载地址列表
查看>>
使用ManageEngine NetFlow Analyzer监控netflow
查看>>
Struts2 漏洞彻底解决办法
查看>>
暖心的回复
查看>>
6月又过去一大半了。
查看>>
分布式文件系统MogileFS介绍
查看>>