最后的项目结构:
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 ListuserModelList = 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