oauth2其實就是在security上在加一層html
一。系統頁登陸前端
導入security包redis
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency>
propertiesspring
server.port=9002數據庫
配置configapp
@Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private UserDetailService userDetailService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailService); } }
配置UserServer用於驗證帳號 loadUserByUsername這裏是直接寫死的返回個User 能夠直接替換成redis 或者數據庫 看我的需求ide
@Service(value = "userDetailService") public class UserDetailService implements UserDetailsService { @Autowired private PasswordEncoder passwordEncode; public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { System.out.println("username=" + username); List<GrantedAuthority> list = new ArrayList<GrantedAuthority>(); list.add(new SimpleGrantedAuthority("ROLE_USER")); User auth_user = new User("test", passwordEncode.encode("123456"), list); return auth_user; } @Bean PasswordEncoder passwordEncoder() { return PasswordEncoderFactories.createDelegatingPasswordEncoder();
} }
配置個Controller用於跳轉post
@RestController public class OAuthController { @RequestMapping("/") String home(HttpServletRequest req,HttpServletResponse res) {return "hello world"; } }
默認使用的是自帶的登陸頁面測試
訪問地址http://localhost:9002/loginui
輸入上面的帳號 test 密碼 123456
登陸成功
二。自定也登陸頁面
@Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private UserDetailService userDetailService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailService); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/","/hello","/login2","/aouth").permitAll() //指定不須要驗證的頁面,其餘的默認會跳轉到登陸頁 .anyRequest() .authenticated() .and() .formLogin() //支持表單提交 .loginPage("/login2").permitAll() //自定義登陸頁面 .failureForwardUrl("/error") //自定也錯誤 .loginProcessingUrl("/login") //提交action 也就是form表單中的action login會調用security的登陸不用本身實現 .successForwardUrl("/hello") //登陸成功頁面 .and().logout() .permitAll(); // System.out.println(http.toString()); } }
前端登陸頁面
_csrf.token 這個很是重要 否則源碼攔截器中默認會把response的response.isCommitted() 設爲true 致使無限返回錯誤頁面
isCommitted 在數據輸出前是false 數據輸出完成爲true
login.ftl 放在templete下面
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Spring Security Example </title> </head> <body> <form action="login" method="post"> <div><label> 用戶名 : <input type="text" name="username" style="width:30%;height:100px;" value="dikeboy"/> </label></div> <div><label> 密 碼 : <input type="password" name="password" style="width:30%;height:100px;" value="123456"/> </label></div> <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"> <div><input type="submit" value="登陸" style="width:30%;height:100px;"/></div> </form> </body> </html>
定義個Controller
@Controller public class WebController { @RequestMapping("/") public ModelAndView Add(HttpServletRequest request,HttpServletResponse response){ Map<String,String> map =new HashMap<String,String>(); map.put("name", "zhangshan"); map.put("link","/login"); ModelAndView mv = new ModelAndView(); mv.setViewName("index"); mv.addObject("user",map); return mv; } @RequestMapping("/hello") public String hello() { System.out.println("hello"); return "hello"; } @RequestMapping("/login2") public String login() { System.out.println("login"); return "mlogin"; } }
其它幾個WEB頁面都比較簡單 隨便弄就行 測試
localhost:9092
登陸成功