作者 朱兆平

spring security 用户登录部分完成

@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 .mvn 5 .mvn
6 /lib/ 6 /lib/
7 /xmlFromImf/ 7 /xmlFromImf/
  8 +kakoRevice/
8 /errorLogs/ 9 /errorLogs/
9 ### STS ### 10 ### STS ###
10 .apt_generated 11 .apt_generated
1 #登录账号 1 #登录账号
2 -loginname = NMMS 2 +loginname = KAKO
3 #登录密码 3 #登录密码
4 -loginpass = NMMS 4 +loginpass = KAKO
5 #发送报文目录,相对程序目录 5 #发送报文目录,相对程序目录
6 -readDirectory = /Users/mrz/Downloads/1.woowo 6 +readDirectory = /Users/mrz/Documents/java项目/test
7 #接收存储报文目录 7 #接收存储报文目录
8 -bakDirectory = xmlFromImf 8 +bakDirectory = kakoRevice
9 #是否需要发送报文,默认N不发,Y将发送readDirectory下的XML扩展名的报文 9 #是否需要发送报文,默认N不发,Y将发送readDirectory下的XML扩展名的报文
10 -isNeedSend = N 10 +isNeedSend = Y
11 11
12 #IMF MEAT报头配置 12 #IMF MEAT报头配置
13 13
@@ -30,6 +30,11 @@ @@ -30,6 +30,11 @@
30 <artifactId>mybatis-spring-boot-starter</artifactId> 30 <artifactId>mybatis-spring-boot-starter</artifactId>
31 <version>1.3.2</version> 31 <version>1.3.2</version>
32 </dependency> 32 </dependency>
  33 + <dependency>
  34 + <groupId>org.springframework.boot</groupId>
  35 + <artifactId>spring-boot-devtools</artifactId>
  36 + <optional>true</optional>
  37 + </dependency>
33 <!--有WEBSOCKET包 包含了spring-boot-starter-web 和spring-boot-starter包 有这个包不要引入这俩包--> 38 <!--有WEBSOCKET包 包含了spring-boot-starter-web 和spring-boot-starter包 有这个包不要引入这俩包-->
34 <dependency> 39 <dependency>
35 <groupId>org.springframework.boot</groupId> 40 <groupId>org.springframework.boot</groupId>
@@ -6,9 +6,12 @@ package com.tianbo.warehouse; @@ -6,9 +6,12 @@ package com.tianbo.warehouse;
6 6
7 import org.mybatis.spring.annotation.MapperScan; 7 import org.mybatis.spring.annotation.MapperScan;
8 import org.springframework.boot.SpringApplication; 8 import org.springframework.boot.SpringApplication;
  9 +import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
9 import org.springframework.boot.autoconfigure.SpringBootApplication; 10 import org.springframework.boot.autoconfigure.SpringBootApplication;
  11 +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
10 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 12 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
11 import org.springframework.scheduling.annotation.EnableScheduling; 13 import org.springframework.scheduling.annotation.EnableScheduling;
  14 +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
12 15
13 @SpringBootApplication 16 @SpringBootApplication
14 @EnableScheduling 17 @EnableScheduling
  1 +package com.tianbo.warehouse.controller;
  2 +
  3 +import com.tianbo.warehouse.model.USERS;
  4 +import com.tianbo.warehouse.service.UserService;
  5 +import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.web.bind.annotation.GetMapping;
  7 +import org.springframework.web.bind.annotation.RestController;
  8 +
  9 +import java.util.List;
  10 +
  11 +@RestController
  12 +public class AdminController {
  13 +
  14 + @Autowired
  15 + UserService userService;
  16 +
  17 + @GetMapping("/admin")
  18 + public List<USERS> admin(){
  19 + return userService.selectAllUser();
  20 + }
  21 +}
1 package com.tianbo.warehouse.controller; 1 package com.tianbo.warehouse.controller;
2 2
3 import com.tianbo.warehouse.model.PERMISSION; 3 import com.tianbo.warehouse.model.PERMISSION;
  4 +import com.tianbo.warehouse.model.USERS;
  5 +import com.tianbo.warehouse.service.UserService;
  6 +import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Controller; 7 import org.springframework.stereotype.Controller;
5 import org.springframework.ui.Model; 8 import org.springframework.ui.Model;
6 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.ResponseBody; 10 import org.springframework.web.bind.annotation.ResponseBody;
8 11
  12 +import java.util.List;
  13 +
9 @Controller 14 @Controller
10 public class ImfLog { 15 public class ImfLog {
11 16
  17 + @Autowired
  18 + UserService userService;
  19 +
12 @RequestMapping("/log/imf") 20 @RequestMapping("/log/imf")
13 public String IMFlog(){ 21 public String IMFlog(){
14 return "log/imf"; 22 return "log/imf";
@@ -24,15 +32,11 @@ public class ImfLog { @@ -24,15 +32,11 @@ public class ImfLog {
24 return "home"; 32 return "home";
25 } 33 }
26 34
27 - @RequestMapping("/admin") 35 + @RequestMapping("/logs")
28 @ResponseBody 36 @ResponseBody
29 - public String Admin(){  
30 - return "hello admin";  
31 - }  
32 -  
33 - @RequestMapping("/login")  
34 - public String login(){  
35 - return "login"; 37 + public List<USERS> logs(){
  38 + List<USERS> usersList =userService.selectAllUser();
  39 + return usersList;
36 } 40 }
37 } 41 }
38 42
@@ -11,7 +11,8 @@ public class IndexController { @@ -11,7 +11,8 @@ public class IndexController {
11 11
12 @GetMapping("index") 12 @GetMapping("index")
13 public String index(){ 13 public String index(){
14 - return "数据仓库接收,落地报文给新舱单"; 14 + return "forward:/login";
  15 +// return "数据仓库接收,落地报文给新舱单";
15 } 16 }
16 17
17 18
  1 +package com.tianbo.warehouse.controller;
  2 +
  3 +import org.springframework.stereotype.Controller;
  4 +import org.springframework.web.bind.annotation.RequestMapping;
  5 +
  6 +@Controller
  7 +public class Login {
  8 +
  9 + @RequestMapping("/login")
  10 + public String login(){
  11 + return "login";
  12 + }
  13 +}
  1 +package com.tianbo.warehouse.controller;
  2 +
  3 +import com.tianbo.warehouse.model.USERS;
  4 +import com.tianbo.warehouse.service.UserService;
  5 +import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.web.bind.annotation.GetMapping;
  7 +import org.springframework.web.bind.annotation.RestController;
  8 +
  9 +import java.util.List;
  10 +
  11 +@RestController
  12 +public class MainController {
  13 +
  14 + @Autowired
  15 + UserService userService;
  16 +
  17 + @GetMapping("/error")
  18 + public List<USERS> error(){
  19 + List<USERS> usersList =userService.selectAllUser();
  20 + return usersList;
  21 + }
  22 +
  23 + @GetMapping("/main")
  24 + public List<USERS> me(){
  25 + List<USERS> usersList =userService.selectAllUser();
  26 + return usersList;
  27 + }
  28 +}
@@ -20,4 +20,6 @@ public interface USERSMapper { @@ -20,4 +20,6 @@ public interface USERSMapper {
20 20
21 int updateByPrimaryKey(USERS record); 21 int updateByPrimaryKey(USERS record);
22 22
  23 + List<USERS> selectAllUser();
  24 +
23 } 25 }
@@ -9,8 +9,7 @@ import org.apache.commons.io.FileUtils; @@ -9,8 +9,7 @@ import org.apache.commons.io.FileUtils;
9 import org.apache.log4j.Logger; 9 import org.apache.log4j.Logger;
10 import org.xml.sax.SAXParseException; 10 import org.xml.sax.SAXParseException;
11 11
12 -import java.io.File;  
13 -import java.io.UnsupportedEncodingException; 12 +import java.io.*;
14 import java.util.Iterator; 13 import java.util.Iterator;
15 import java.util.List; 14 import java.util.List;
16 15
1 package com.tianbo.warehouse.model; 1 package com.tianbo.warehouse.model;
2 2
  3 +import org.springframework.security.core.GrantedAuthority;
  4 +
3 import java.math.BigDecimal; 5 import java.math.BigDecimal;
4 6
5 -public class PERMISSION { 7 +public class PERMISSION implements GrantedAuthority{
  8 +
  9 + private static final long serialVersionUID = -3957539165716897200L;
  10 +
6 private BigDecimal permissionId; 11 private BigDecimal permissionId;
7 12
8 private String permissionName; 13 private String permissionName;
@@ -52,4 +57,9 @@ public class PERMISSION { @@ -52,4 +57,9 @@ public class PERMISSION {
52 public void setGroupName(String groupName) { 57 public void setGroupName(String groupName) {
53 this.groupName = groupName == null ? null : groupName.trim(); 58 this.groupName = groupName == null ? null : groupName.trim();
54 } 59 }
  60 +
  61 + @Override
  62 + public String getAuthority(){
  63 + return this.getPermissionName();
  64 + }
55 } 65 }
@@ -4,9 +4,9 @@ import org.springframework.security.core.GrantedAuthority; @@ -4,9 +4,9 @@ import org.springframework.security.core.GrantedAuthority;
4 4
5 import java.math.BigDecimal; 5 import java.math.BigDecimal;
6 6
7 -public class ROLE implements GrantedAuthority{ 7 +public class ROLE{
  8 +
8 9
9 - private static final long serialVersionUID = -3957539165716897200L;  
10 10
11 private BigDecimal roleId; 11 private BigDecimal roleId;
12 12
@@ -48,8 +48,5 @@ public class ROLE implements GrantedAuthority{ @@ -48,8 +48,5 @@ public class ROLE implements GrantedAuthority{
48 this.description = description == null ? null : description.trim(); 48 this.description = description == null ? null : description.trim();
49 } 49 }
50 50
51 - @Override  
52 - public String getAuthority(){  
53 - return this.getRoleName();  
54 - } 51 +
55 } 52 }
@@ -37,6 +37,8 @@ public class USERS implements UserDetails{ @@ -37,6 +37,8 @@ public class USERS implements UserDetails{
37 37
38 private List<ROLE> roles; 38 private List<ROLE> roles;
39 39
  40 + private List<PERMISSION> permissions;
  41 +
40 public BigDecimal getUserId() { 42 public BigDecimal getUserId() {
41 return userId; 43 return userId;
42 } 44 }
@@ -127,6 +129,13 @@ public class USERS implements UserDetails{ @@ -127,6 +129,13 @@ public class USERS implements UserDetails{
127 this.roles = roles; 129 this.roles = roles;
128 } 130 }
129 131
  132 + public List<PERMISSION> getPermissions() {
  133 + return permissions;
  134 + }
  135 +
  136 + public void setPermissions(List<PERMISSION> permissions) {
  137 + this.permissions = permissions;
  138 + }
130 @Override 139 @Override
131 public boolean isAccountNonExpired(){ 140 public boolean isAccountNonExpired(){
132 return true; 141 return true;
@@ -150,9 +159,9 @@ public class USERS implements UserDetails{ @@ -150,9 +159,9 @@ public class USERS implements UserDetails{
150 @Override 159 @Override
151 public Collection<? extends GrantedAuthority> getAuthorities(){ 160 public Collection<? extends GrantedAuthority> getAuthorities(){
152 List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>(); 161 List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
153 - List<ROLE> roles = this.getRoles();  
154 - for (ROLE role : roles) {  
155 - auths.add(new SimpleGrantedAuthority(role.getAuthority())); 162 + List<PERMISSION> permissions = this.getPermissions();
  163 + for (PERMISSION permission : permissions) {
  164 + auths.add(new SimpleGrantedAuthority(permission.getAuthority()));
156 } 165 }
157 return auths; 166 return auths;
158 } 167 }
@@ -24,7 +24,7 @@ public class IMF_Task { @@ -24,7 +24,7 @@ public class IMF_Task {
24 public static String isNeedSend = "N"; 24 public static String isNeedSend = "N";
25 25
26 26
27 - @Scheduled(fixedRate = 5000) 27 +// @Scheduled(fixedRate = 5000)
28 private static void start() throws Exception { 28 private static void start() throws Exception {
29 PropertyConfigurator.configure("config/log4j.properties"); 29 PropertyConfigurator.configure("config/log4j.properties");
30 client = IMFClientFactory.createInstance(); 30 client = IMFClientFactory.createInstance();
@@ -41,6 +41,7 @@ public class IMF_Task { @@ -41,6 +41,7 @@ public class IMF_Task {
41 //启动读取线程 41 //启动读取线程
42 if (client != null) { 42 if (client != null) {
43 IMF_Reader reader = new IMF_Reader(client); 43 IMF_Reader reader = new IMF_Reader(client);
  44 +
44 if (!IMF_Reader.isrunning) { 45 if (!IMF_Reader.isrunning) {
45 reader.start(); 46 reader.start();
46 logger.info("*********读取线程已开启***********"); 47 logger.info("*********读取线程已开启***********");
1 package com.tianbo.warehouse.security; 1 package com.tianbo.warehouse.security;
2 2
3 -import com.tianbo.warehouse.dao.PERMISSIONMapper;  
4 -import com.tianbo.warehouse.dao.USERSMapper;  
5 -import com.tianbo.warehouse.model.PERMISSION;  
6 import com.tianbo.warehouse.model.USERS; 3 import com.tianbo.warehouse.model.USERS;
7 import com.tianbo.warehouse.service.UserService; 4 import com.tianbo.warehouse.service.UserService;
8 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Autowired;
9 -import org.springframework.security.core.GrantedAuthority;  
10 -import org.springframework.security.core.authority.SimpleGrantedAuthority;  
11 -import org.springframework.security.core.userdetails.User;  
12 import org.springframework.security.core.userdetails.UserDetails; 6 import org.springframework.security.core.userdetails.UserDetails;
13 import org.springframework.security.core.userdetails.UserDetailsService; 7 import org.springframework.security.core.userdetails.UserDetailsService;
14 import org.springframework.security.core.userdetails.UsernameNotFoundException; 8 import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -22,26 +16,12 @@ public class CustomUserDetailService implements UserDetailsService { @@ -22,26 +16,12 @@ public class CustomUserDetailService implements UserDetailsService {
22 16
23 @Autowired 17 @Autowired
24 UserService userService; 18 UserService userService;
25 - @Autowired  
26 - PERMISSIONMapper permissionMapper;  
27 19
28 @Override 20 @Override
29 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 21 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
30 22
31 try { 23 try {
32 USERS user = userService.loadByUsername(username); 24 USERS user = userService.loadByUsername(username);
33 -  
34 - //这里是根据用户ID取的权限表,还可以根据用户ID 去角色表  
35 - List<PERMISSION> permissions = permissionMapper.findByUserId(user.getUserId());  
36 - List<GrantedAuthority> grantedAuthorities = new ArrayList<>();  
37 - for (PERMISSION permission : permissions) {  
38 - if (permission != null && permission.getPermissionName() != null) {  
39 -  
40 - GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(permission.getPermissionName());  
41 - //1:此处将权限信息添加到 GrantedAuthority 对象中,在后面进行全权限验证时会使用GrantedAuthority 对象。  
42 - grantedAuthorities.add(grantedAuthority);  
43 - }  
44 - }  
45 return user; 25 return user;
46 } catch (UsernameNotFoundException e) { 26 } catch (UsernameNotFoundException e) {
47 27
1 package com.tianbo.warehouse.security; 1 package com.tianbo.warehouse.security;
2 2
3 import org.springframework.beans.factory.annotation.Autowired; 3 import org.springframework.beans.factory.annotation.Autowired;
4 -import org.springframework.context.annotation.Bean; 4 +import org.springframework.beans.factory.annotation.Qualifier;
5 import org.springframework.context.annotation.Configuration; 5 import org.springframework.context.annotation.Configuration;
6 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 7 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -21,29 +21,48 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @@ -21,29 +21,48 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
21 @Autowired 21 @Autowired
22 private PasswordEncoder passwordEncoder; 22 private PasswordEncoder passwordEncoder;
23 23
24 - @Bean  
25 - UserDetailsService customUserService(){ //注册UserDetailsService 的bean  
26 - return new CustomUserDetailService();  
27 - } 24 + @Qualifier("customuserservice")
  25 + @Autowired
  26 + private UserDetailsService userDetailsService;
28 27
29 @Override 28 @Override
30 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 29 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
31 //user Details Service验证 30 //user Details Service验证
32 - auth.userDetailsService(customUserService()).passwordEncoder(passwordEncoder); 31 + auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
33 } 32 }
34 33
35 @Override 34 @Override
36 protected void configure(HttpSecurity http) throws Exception { 35 protected void configure(HttpSecurity http) throws Exception {
37 http.authorizeRequests() 36 http.authorizeRequests()
38 - .anyRequest().authenticated() //任何请求,登录后可以访问 37 + .antMatchers("/admin","/role").authenticated()
  38 + //管理页面只允许管理员角色访问 //任何请求,登录后可以访问
  39 + .anyRequest().permitAll() //其余的不需要验证
39 .and() 40 .and()
40 .formLogin() 41 .formLogin()
41 - .loginProcessingUrl("/home") 42 + .passwordParameter("password")
  43 + .usernameParameter("username")
  44 + //.loginProcessingUrl("/home")//登陆提交的处理url
42 .loginPage("/login") 45 .loginPage("/login")
43 - .failureUrl("/login?error")  
44 - .permitAll() //登录页面用户任意访问 46 + .failureUrl("/error")
  47 + .permitAll()//登录页面用户任意访问
  48 + .successForwardUrl("/main")
  49 + .and()
  50 + .logout()
  51 + .logoutSuccessUrl("/?logout=true")
  52 + .permitAll()
45 .and() 53 .and()
46 - .logout().permitAll(); //注销行为任意访问  
47 - http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class); 54 + .rememberMe()
  55 + .tokenValiditySeconds(604800)
  56 + //记住我功能,cookies有限期是一周
  57 + .rememberMeParameter("remeberme")
  58 + //登陆时是否激活记住我功能的参数名字,在登陆页面有展示
  59 + .rememberMeCookieName("workspace")
  60 + //cookies的名字,登陆后可以通过浏览器查看cookies名字
  61 + .and()
  62 + .cors()
  63 + .and()
  64 + .csrf().disable();
  65 +
  66 + //http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class).csrf().disable();
48 } 67 }
49 } 68 }
@@ -2,6 +2,9 @@ package com.tianbo.warehouse.service; @@ -2,6 +2,9 @@ package com.tianbo.warehouse.service;
2 2
3 import com.tianbo.warehouse.model.USERS; 3 import com.tianbo.warehouse.model.USERS;
4 4
  5 +import java.util.List;
  6 +
5 public interface UserService { 7 public interface UserService {
6 USERS loadByUsername(String username); 8 USERS loadByUsername(String username);
  9 + List<USERS> selectAllUser();
7 } 10 }
1 package com.tianbo.warehouse.service.imp; 1 package com.tianbo.warehouse.service.imp;
2 2
  3 +import com.tianbo.warehouse.dao.PERMISSIONMapper;
3 import com.tianbo.warehouse.dao.ROLEMapper; 4 import com.tianbo.warehouse.dao.ROLEMapper;
4 import com.tianbo.warehouse.dao.USERSMapper; 5 import com.tianbo.warehouse.dao.USERSMapper;
  6 +import com.tianbo.warehouse.model.PERMISSION;
5 import com.tianbo.warehouse.model.ROLE; 7 import com.tianbo.warehouse.model.ROLE;
6 import com.tianbo.warehouse.model.USERS; 8 import com.tianbo.warehouse.model.USERS;
7 import com.tianbo.warehouse.service.UserService; 9 import com.tianbo.warehouse.service.UserService;
@@ -19,6 +21,9 @@ public class UserServiceImpl implements UserService{ @@ -19,6 +21,9 @@ public class UserServiceImpl implements UserService{
19 @Autowired 21 @Autowired
20 private ROLEMapper roleMapper; 22 private ROLEMapper roleMapper;
21 23
  24 + @Autowired
  25 + private PERMISSIONMapper permissionMapper;
  26 +
22 @Override 27 @Override
23 public USERS loadByUsername(String username){ 28 public USERS loadByUsername(String username){
24 List<USERS> userList = usersMapper.selectByUsername(username); 29 List<USERS> userList = usersMapper.selectByUsername(username);
@@ -26,13 +31,35 @@ public class UserServiceImpl implements UserService{ @@ -26,13 +31,35 @@ public class UserServiceImpl implements UserService{
26 if (userList != null && userList.size() > 0) { 31 if (userList != null && userList.size() > 0) {
27 USERS user = userList.get(0); 32 USERS user = userList.get(0);
28 33
  34 + List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId());
  35 + if (permissionList!=null && permissionList.size()>0){
  36 + user.setPermissions(permissionList);
  37 + }
  38 +
29 List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId()); 39 List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId());
30 if (roleList!=null && roleList.size()>0){ 40 if (roleList!=null && roleList.size()>0){
31 user.setRoles(roleList); 41 user.setRoles(roleList);
32 - return user;  
33 } 42 }
  43 + return user;
34 } 44 }
35 return null; 45 return null;
36 46
37 } 47 }
  48 +
  49 + @Override
  50 + public List<USERS> selectAllUser(){
  51 + List<USERS> list = usersMapper.selectAllUser();
  52 + for (USERS user: list) {
  53 + List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId());
  54 + if (permissionList!=null && permissionList.size()>0){
  55 + user.setPermissions(permissionList);
  56 + }
  57 +
  58 + List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId());
  59 + if (roleList!=null && roleList.size()>0){
  60 + user.setRoles(roleList);
  61 + }
  62 + }
  63 + return list;
  64 + }
38 } 65 }
@@ -25,6 +25,7 @@ public class MakeImfMeta { @@ -25,6 +25,7 @@ public class MakeImfMeta {
25 meta.addElement("STYP").addText(STYP); 25 meta.addElement("STYP").addText(STYP);
26 26
27 SAXReader saxReader = new SAXReader(); 27 SAXReader saxReader = new SAXReader();
  28 +// saxReader.setEncoding("gb2312");
28 Document doc = saxReader.read(Content); 29 Document doc = saxReader.read(Content);
29 Element contentRoot = doc.getRootElement(); 30 Element contentRoot = doc.getRootElement();
30 root.add(contentRoot); 31 root.add(contentRoot);
@@ -41,10 +41,14 @@ spring.datasource.druid.test-on-borrow=false @@ -41,10 +41,14 @@ spring.datasource.druid.test-on-borrow=false
41 spring.datasource.druid.test-on-return=false 41 spring.datasource.druid.test-on-return=false
42 spring.datasource.druid.default-auto-commit=true 42 spring.datasource.druid.default-auto-commit=true
43 43
  44 +#security配置
  45 +trace=true
  46 +
44 mybatis.mapper-locations=classpath:mapping/*.xml 47 mybatis.mapper-locations=classpath:mapping/*.xml
45 mybatis.type-aliases-package=com.tianbo.warehouse.model 48 mybatis.type-aliases-package=com.tianbo.warehouse.model
46 logging.level.com.tianbo.warehouse.dao=DEBUG 49 logging.level.com.tianbo.warehouse.dao=DEBUG
47 - 50 +logging.level.org.springframework.security =debug
  51 +debug=true
48 pagehelper.helper-dialect=oracle 52 pagehelper.helper-dialect=oracle
49 #pagehelper.auto-dialect=true 53 #pagehelper.auto-dialect=true
50 #pagehelper.auto-runtime-dialect=true 54 #pagehelper.auto-runtime-dialect=true
@@ -23,13 +23,33 @@ @@ -23,13 +23,33 @@
23 from PERMISSION 23 from PERMISSION
24 </select> 24 </select>
25 <select id="findByUserId" parameterType="java.math.BigDecimal" resultMap="BaseResultMap"> 25 <select id="findByUserId" parameterType="java.math.BigDecimal" resultMap="BaseResultMap">
26 - select p.*  
27 - from USERS u  
28 - LEFT JOIN USER_ROLE ur on u.user_id= ur.user_id  
29 - LEFT JOIN ROLE r on ur.role_id=r.role_id  
30 - LEFT JOIN ROLE_PERMISSION rp on rp.role_id=r.role_id  
31 - LEFT JOIN PERMISSION p on p.PERMISSION_ID =rp.permission_id  
32 - where u.USER_ID = #{userId,jdbcType=DECIMAL} 26 + SELECT
  27 + P .*
  28 +FROM
  29 + PERMISSION P
  30 +WHERE
  31 + P .PERMISSION_ID IN (
  32 + SELECT
  33 + RP.permission_id
  34 + FROM
  35 + ROLE_PERMISSION RP
  36 + WHERE
  37 + RP.role_id IN (
  38 + SELECT
  39 + R.ROLE_ID
  40 + FROM
  41 + ROLE R
  42 + WHERE
  43 + R.ROLE_ID IN (
  44 + SELECT
  45 + UR.ROLE_ID
  46 + FROM
  47 + USER_ROLE UR
  48 + WHERE
  49 + UR.USER_ID = #{userId,jdbcType=DECIMAL}
  50 + )
  51 + )
  52 + )
33 </select> 53 </select>
34 54
35 <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal" > 55 <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal" >
@@ -24,10 +24,15 @@ @@ -24,10 +24,15 @@
24 where USER_ID = #{userId,jdbcType=DECIMAL} 24 where USER_ID = #{userId,jdbcType=DECIMAL}
25 </select> 25 </select>
26 <select id="selectByUsername" resultMap="BaseResultMap" parameterType="java.lang.String" > 26 <select id="selectByUsername" resultMap="BaseResultMap" parameterType="java.lang.String" >
  27 + select
  28 + <include refid="Base_Column_List" />
  29 + from USERS
  30 + where USERNAME = #{username,jdbcType=VARCHAR}
  31 +</select>
  32 + <select id="selectAllUser" resultMap="BaseResultMap" >
27 select 33 select
28 <include refid="Base_Column_List" /> 34 <include refid="Base_Column_List" />
29 from USERS 35 from USERS
30 - where USERNAME = #{username,jdbcType=VARCHAR}  
31 </select> 36 </select>
32 <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal" > 37 <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal" >
33 delete from USERS 38 delete from USERS
  1 +<!DOCTYPE html>
  2 +<html xmlns:th="http://www.thymeleaf.org">
  3 +<head>
  4 + <meta content="text/html;charset=UTF-8"/>
  5 + <title>登录页面</title>
  6 + <link rel="stylesheet" th:href="@{css/bootstrap.min.css}"/>
  7 + <style type="text/css">
  8 + body {
  9 + padding-top: 50px;
  10 + }
  11 + .starter-template {
  12 + padding: 40px 15px;
  13 + text-align: center;
  14 + }
  15 + </style>
  16 +</head>
  17 +<body>
  18 +
  19 +<nav class="navbar navbar-inverse navbar-fixed-top">
  20 + <div class="container">
  21 + <div class="navbar-header">
  22 + <a class="navbar-brand" href="#">Spring Security演示</a>
  23 + </div>
  24 + <div id="navbar" class="collapse navbar-collapse">
  25 + <ul class="nav navbar-nav">
  26 + <li><a th:href="@{/}"> 首页 </a></li>
  27 +
  28 + </ul>
  29 + </div><!--/.nav-collapse -->
  30 + </div>
  31 +</nav>
  32 +<div class="container">
  33 +
  34 + <div class="starter-template">
  35 + <p th:if="${param.logout}" class="bg-warning">已成功注销</p><!-- 1 -->
  36 + <p th:if="${param.error}" class="bg-danger">有错误,请重试</p> <!-- 2 -->
  37 + <h2>使用账号密码登录</h2>
  38 + <form name="form" th:action="@{/login}" action="/login" method="POST"> <!-- 3 -->
  39 + <div class="form-group">
  40 + <label for="username">账号</label>
  41 + <input type="text" class="form-control" name="username" value="" placeholder="账号" />
  42 + </div>
  43 + <div class="form-group">
  44 + <label for="password">密码</label>
  45 + <input type="password" class="form-control" name="password" placeholder="密码" />
  46 + </div>
  47 + <input type="submit" id="login" value="Login" class="btn btn-primary" />
  48 + </form>
  49 + </div>
  50 +</div>
  51 +</body>
  52 +</html>
  1 +<!DOCTYPE html>
  2 +<html xmlns:th="http://www.thymeleaf.org">
  3 +<head>
  4 + <meta content="text/html;charset=UTF-8"/>
  5 + <title>错误页面</title>
  6 + <link rel="stylesheet" th:href="@{css/bootstrap.min.css}"/>
  7 + <style type="text/css">
  8 + body {
  9 + padding-top: 50px;
  10 + }
  11 + .starter-template {
  12 + padding: 40px 15px;
  13 + text-align: center;
  14 + }
  15 + </style>
  16 +</head>
  17 +<body>
  18 +
  19 +<nav class="navbar navbar-inverse navbar-fixed-top">
  20 + <div class="container">
  21 + <div class="navbar-header">
  22 + <a class="navbar-brand" href="#">Spring Security演示</a>
  23 + </div>
  24 + <div id="navbar" class="collapse navbar-collapse">
  25 + <ul class="nav navbar-nav">
  26 + <li><a th:href="@{/}"> 首页 </a></li>
  27 +
  28 + </ul>
  29 + </div><!--/.nav-collapse -->
  30 + </div>
  31 +</nav>
  32 +<div class="container">
  33 +
  34 +</div>
  35 +</body>
  36 +</html>
@@ -36,10 +36,10 @@ @@ -36,10 +36,10 @@
36 <div class="starter-template"> 36 <div class="starter-template">
37 <h1 th:text="${msg.description}"></h1> 37 <h1 th:text="${msg.description}"></h1>
38 38
39 - <p class="bg-primary" th:text="${msg.content}"></p> 39 + <p class="bg-primary" th:text="${msg.description}"></p>
40 40
41 <div sec:authorize="hasRole('ROLE_HOME')"> <!-- 用户类型为ROLE_ADMIN 显示 --> 41 <div sec:authorize="hasRole('ROLE_HOME')"> <!-- 用户类型为ROLE_ADMIN 显示 -->
42 - <p class="bg-info" th:text="${msg.etraInfo}"></p> 42 + <p class="bg-info" th:text="${msg.description}"></p>
43 </div> 43 </div>
44 <div sec:authorize="hasRole('ROLE_ADMIN')"> <!-- 用户类型为ROLE_ADMIN 显示 --> 44 <div sec:authorize="hasRole('ROLE_ADMIN')"> <!-- 用户类型为ROLE_ADMIN 显示 -->
45 <p class="bg-info">恭喜您,您有 ROLE_ADMIN 权限 </p> 45 <p class="bg-info">恭喜您,您有 ROLE_ADMIN 权限 </p>
@@ -44,6 +44,10 @@ @@ -44,6 +44,10 @@
44 <label for="password">密码</label> 44 <label for="password">密码</label>
45 <input type="password" class="form-control" name="password" placeholder="密码" /> 45 <input type="password" class="form-control" name="password" placeholder="密码" />
46 </div> 46 </div>
  47 + <div class="form-group">
  48 + <label for="remeberme">记住我</label>
  49 + <input type="checkbox" name="remember-me" />
  50 + </div>
47 <input type="submit" id="login" value="Login" class="btn btn-primary" /> 51 <input type="submit" id="login" value="Login" class="btn btn-primary" />
48 </form> 52 </form>
49 </div> 53 </div>