From 2983602e25d70698b6d6c8d0cfcf4a12977d01c6 Mon Sep 17 00:00:00 2001 From: lijiantao Date: Wed, 12 Jun 2019 18:05:24 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0shiro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- antdsp-admin/.gitignore | 1 + .../com/antdsp/common/AntdspResponse.java | 2 +- .../java/com/antdsp/common/ResponseCode.java | 1 + .../configuration/ShiroConfiguration.java | 70 ++ .../antdsp/common/shiro/AntdspShiroRealm.java | 54 ++ .../com/antdsp/web/dto/LoginUserInfo.java | 21 + .../main/java/com/antdsp/web/rest/Login.java | 59 +- .../src/main/resources/application-dev.yml | 2 +- .../src/main/web/config/router.config.js | 11 +- .../src/main/web/src/pages/Login/Index.js | 60 ++ .../main/web/src/pages/System/User/Index.js | 715 +++++++++--------- antdsp-core/.gitignore | 1 + .../antdsp/data/entityeenum/UserStatus.java | 1 + 13 files changed, 649 insertions(+), 349 deletions(-) create mode 100644 antdsp-admin/.gitignore create mode 100644 antdsp-admin/src/main/java/com/antdsp/common/configuration/ShiroConfiguration.java create mode 100644 antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java create mode 100644 antdsp-admin/src/main/java/com/antdsp/web/dto/LoginUserInfo.java create mode 100644 antdsp-admin/src/main/web/src/pages/Login/Index.js create mode 100644 antdsp-core/.gitignore diff --git a/antdsp-admin/.gitignore b/antdsp-admin/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/antdsp-admin/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/antdsp-admin/src/main/java/com/antdsp/common/AntdspResponse.java b/antdsp-admin/src/main/java/com/antdsp/common/AntdspResponse.java index 284b337..05ffe94 100644 --- a/antdsp-admin/src/main/java/com/antdsp/common/AntdspResponse.java +++ b/antdsp-admin/src/main/java/com/antdsp/common/AntdspResponse.java @@ -59,7 +59,7 @@ public class AntdspResponse { } public static AntdspResponse error(String msg) { - return error(ResponseCode.REJECT , msg); + return error(ResponseCode.ERROR , msg); } public static AntdspResponse error(ResponseCode code , String msg) { diff --git a/antdsp-admin/src/main/java/com/antdsp/common/ResponseCode.java b/antdsp-admin/src/main/java/com/antdsp/common/ResponseCode.java index 91ab81d..cd7fc48 100644 --- a/antdsp-admin/src/main/java/com/antdsp/common/ResponseCode.java +++ b/antdsp-admin/src/main/java/com/antdsp/common/ResponseCode.java @@ -3,6 +3,7 @@ package com.antdsp.common; public enum ResponseCode { OK(1200 , "正常"), + ERROR(1201,"未正常处理"), REJECT(1207 , "拒绝"), UNAUTHORIZED(1401 , "未登录"), FORBIDDEN(1403 , "权限不足"); diff --git a/antdsp-admin/src/main/java/com/antdsp/common/configuration/ShiroConfiguration.java b/antdsp-admin/src/main/java/com/antdsp/common/configuration/ShiroConfiguration.java new file mode 100644 index 0000000..7658d6b --- /dev/null +++ b/antdsp-admin/src/main/java/com/antdsp/common/configuration/ShiroConfiguration.java @@ -0,0 +1,70 @@ +package com.antdsp.common.configuration; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.shiro.mgt.SecurityManager; +import org.apache.shiro.realm.Realm; +import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; +import org.apache.shiro.spring.web.ShiroFilterFactoryBean; +import org.apache.shiro.web.mgt.DefaultWebSecurityManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.antdsp.common.shiro.AntdspShiroRealm; + +/** + * + *

title: ShiroConfiguration

+ *

Description:

+ *

Copyright: Copyright (c) 2019

+ * + * @author lijiantao + * @date 2019年6月12日 + * @email a496401006@qq.com + * + */ +@Configuration +public class ShiroConfiguration { + + @Bean + public Realm shiroRealm() { + AntdspShiroRealm realm = new AntdspShiroRealm(); + return realm; + } + + @Bean("securityManager") + public SecurityManager securityManager() { + DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); + securityManager.setRealm(shiroRealm()); + return securityManager; + } + + @Bean + public ShiroFilterFactoryBean shirofilterFactoryBean(SecurityManager securityManager) { + ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean(); + shiroFilterFactory.setSecurityManager(securityManager); + + Map filterMap = new HashMap<>(); + filterMap.put("/login","anon"); + filterMap.put("/logout","anon"); + filterMap.put("/unauth","anon"); + filterMap.put("/**","authc"); + + shiroFilterFactory.setFilterChainDefinitionMap(filterMap); + shiroFilterFactory.setSuccessUrl("/login_success"); + shiroFilterFactory.setUnauthorizedUrl("/unauth"); + shiroFilterFactory.setLoginUrl("/login"); + + return shiroFilterFactory; + } + + //使用shiro的权限注解 + @Bean + public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ + AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); + authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); + return authorizationAttributeSourceAdvisor; + } + +} diff --git a/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java b/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java new file mode 100644 index 0000000..ec9d486 --- /dev/null +++ b/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java @@ -0,0 +1,54 @@ +package com.antdsp.common.shiro; + +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.AuthenticationInfo; +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.authc.DisabledAccountException; +import org.apache.shiro.authc.SimpleAuthenticationInfo; +import org.apache.shiro.authc.UnknownAccountException; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.realm.AuthorizingRealm; +import org.apache.shiro.subject.PrincipalCollection; +import org.springframework.beans.factory.annotation.Autowired; + +import com.antdsp.dao.jpa.UserJpa; +import com.antdsp.data.entity.User; +import com.antdsp.data.entityeenum.UserStatus; + +public class AntdspShiroRealm extends AuthorizingRealm{ + + @Autowired + private UserJpa userJpa; + + @Override + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { + + UsernamePasswordToken passwordToken = (UsernamePasswordToken) token; + + String username = passwordToken.getUsername(); + String password = new String(passwordToken.getPassword()); + + User user = userJpa.queryUserByLoginName(username); + if(user == null) { + throw new UnknownAccountException("用户名/密码错误"); + } + if(user.getPassword().equals(password)) { + throw new UnknownAccountException("用户名/密码错误"); + } + if(UserStatus.DISABLED.equals(user.getStatus())) { + throw new DisabledAccountException("用户名已经禁止使用"); + } + + SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user , user.getPassword() , getName()); + + return info; + } + +} diff --git a/antdsp-admin/src/main/java/com/antdsp/web/dto/LoginUserInfo.java b/antdsp-admin/src/main/java/com/antdsp/web/dto/LoginUserInfo.java new file mode 100644 index 0000000..838cda9 --- /dev/null +++ b/antdsp-admin/src/main/java/com/antdsp/web/dto/LoginUserInfo.java @@ -0,0 +1,21 @@ +package com.antdsp.web.dto; + +public class LoginUserInfo { + + private String loginname; + private String password; + + public String getLoginname() { + return loginname; + } + public void setLoginname(String loginname) { + this.loginname = loginname; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + +} diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java index f98e901..6892afb 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java @@ -1,5 +1,62 @@ package com.antdsp.web.rest; -public class Login { +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.Session; +import org.apache.shiro.subject.Subject; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.antdsp.common.AntdspResponse; +import com.antdsp.common.ResponseCode; +import com.antdsp.web.dto.LoginUserInfo; +@RestController +public class Login { + + @GetMapping("/login") + public AntdspResponse login() { + return AntdspResponse.error(ResponseCode.UNAUTHORIZED , "请先登录"); + } + + @PostMapping("/login") + public AntdspResponse login(@RequestBody LoginUserInfo userInfo) { + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getLoginname() , userInfo.getPassword()); + try { + subject.login(token); + Session session = subject.getSession(); + return AntdspResponse.success(session.getId().toString()); + }catch(AuthenticationException e) { + return AntdspResponse.error(e.getMessage()); + } + + } + + @GetMapping("/login_success") + public AntdspResponse loginsuccess() { + Subject subject = SecurityUtils.getSubject(); + Session session = subject.getSession(); + return AntdspResponse.success(session.getId().toString()); + } + + @GetMapping("/unauth") + public AntdspResponse unauth() { + return new AntdspResponse(ResponseCode.FORBIDDEN , false); + } + + @GetMapping("/logout") + public AntdspResponse logout() { + + Subject subject = SecurityUtils.getSubject(); + subject.logout(); + return AntdspResponse.success("退出登录成功"); + } + } diff --git a/antdsp-admin/src/main/resources/application-dev.yml b/antdsp-admin/src/main/resources/application-dev.yml index 99f09a4..5783a6b 100644 --- a/antdsp-admin/src/main/resources/application-dev.yml +++ b/antdsp-admin/src/main/resources/application-dev.yml @@ -2,7 +2,7 @@ spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/antdsp?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai + url: jdbc:mysql://192.168.1.231:3306/antdsp?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456 initial-size: 10 diff --git a/antdsp-admin/src/main/web/config/router.config.js b/antdsp-admin/src/main/web/config/router.config.js index 673bdc8..2246bc7 100644 --- a/antdsp-admin/src/main/web/config/router.config.js +++ b/antdsp-admin/src/main/web/config/router.config.js @@ -1,4 +1,10 @@ export default [ + //login + { + path: '/login', + name: 'login', + component: './Login/Index', + }, // app { path: '/', @@ -7,7 +13,8 @@ export default [ authority: 'admin', routes: [ { - path: '/', redirect: '/system/user', + path: '/', + redirect: '/system/user', }, // dashboard { @@ -32,7 +39,7 @@ export default [ name: 'role', icon: 'team', component: './System/Role/Index', - } + }, ], }, { diff --git a/antdsp-admin/src/main/web/src/pages/Login/Index.js b/antdsp-admin/src/main/web/src/pages/Login/Index.js new file mode 100644 index 0000000..777dc84 --- /dev/null +++ b/antdsp-admin/src/main/web/src/pages/Login/Index.js @@ -0,0 +1,60 @@ +import { PureComponent } from 'react'; +import { Card, Form, Input, Icon } from 'antd'; +import Block from '@/custom/Block'; + +const FormItem = Form.Item; + +@Form.create() +export default class Login extends PureComponent { + render() { + const { + form: { getFieldDecorator }, + } = this.props; + + return ( +
+ {/*
*/} +
+ Antdsp Login Page +
+
+
+ + {getFieldDecorator('loginname', { + rules: [ + { + required: true, + message: '', + }, + ], + })( + } + placeholder="用户名" + /> + )} + + + {getFieldDecorator('password', { + rules: [ + { + required: true, + message: '', + }, + ], + })( + } + type="password" + placeholder="密码∂" + /> + )} + +
+
+ + {/*
*/} +
+ ); + } +} diff --git a/antdsp-admin/src/main/web/src/pages/System/User/Index.js b/antdsp-admin/src/main/web/src/pages/System/User/Index.js index 1185b4c..e82eadf 100644 --- a/antdsp-admin/src/main/web/src/pages/System/User/Index.js +++ b/antdsp-admin/src/main/web/src/pages/System/User/Index.js @@ -1,365 +1,392 @@ -import { PureComponent, Fragment } from "react"; +import { PureComponent, Fragment } from 'react'; import { connect } from 'dva'; import PageHeaderWrapper from '@/components/PageHeaderWrapper'; import Block from '@/custom/Block'; -import { Card, Form, Button, Table, Divider, Input, Select, Modal, message, Popconfirm } from "antd"; +import { + Card, + Form, + Button, + Table, + Divider, + Input, + Select, + Modal, + message, + Popconfirm, +} from 'antd'; const FormItem = Form.Item; const UserStatus = { - "NORMAL": "正常", - "FORBIDDEN": "异常" -} + NORMAL: '正常', + FORBIDDEN: '异常', +}; -@connect(({ systemuser , loading })=>({ - systemuser, - loading: loading.models.systemuser +@connect(({ systemuser, loading }) => ({ + systemuser, + loading: loading.models.systemuser, })) @Form.create() -export default class extends PureComponent{ - - state={ - confirmLoading: false, - visible: false, - current:{}, - formValue: { - page: 1, - count: 10, - } - } - - componentDidMount(){ - const { dispatch } = this.props; - dispatch({ - type: 'systemuser/fetchAll', - payload:{ +export default class extends PureComponent { + state = { + confirmLoading: false, + visible: false, + current: {}, + formValue: { + page: 1, + count: 10, + }, + }; + + componentDidMount() { + const { dispatch } = this.props; + dispatch({ + type: 'systemuser/fetchAll', + payload: { + page: 1, + count: 10, + }, + }); + } + + handlerQueryOnClick = () => { + const { form, dispatch } = this.props; + form.validateFields((err, fieldsValue) => { + let { formValue } = this.state.formValue; + formValue = { + ...fieldsValue, + page: 1, + count: 10, + }; + this.setState({ formValue }); + + dispatch({ + type: 'systemuser/fetchAll', + payload: { + ...formValue, + }, + }); + }); + }; + + renderQueryForm = () => { + const { getFieldDecorator } = this.props.form; + + return ( + +
+ + {getFieldDecorator('loginname')()} + + + {getFieldDecorator('status')( + + )} + + + + +
+
+ ); + }; + + handlerTableOnChange = (pagination, filters, sorter) => { + const { dispatch } = this.props; + let { formValue } = this.state; + + formValue = { + ...formValue, + page: pagination.current, + count: pagination.pageSize, + }; + + this.setState({ + formValue: { + ...formValue, + }, + }); + + dispatch({ + type: 'systemuser/fetchAll', + payload: { + ...formValue, + }, + }); + }; + + showModal = currentObj => { + this.setState({ + current: currentObj, + confirmLoading: false, + visible: true, + }); + }; + + handlerModalOnOk = e => { + e.preventDefault(); + this.setState({ + confirmLoading: true, + }); + const { form } = this.formRef.props; + const { dispatch } = this.props; + form.validateFields((err, fieldsValue) => { + if (err) return this.setState({ confirmLoading: false }); + + const { current } = this.state; + let currentObj = { + ...current, + ...fieldsValue, + }; + dispatch({ + type: 'systemuser/save', + payload: { + user: currentObj, + }, + callback: result => { + if (result.success) { + this.setState({ + confirmLoading: false, + visible: false, + }); + dispatch({ + type: 'systemuser/fetchAll', + payload: { page: 1, count: 10, - } - }) - } - - handlerQueryOnClick=()=>{ - const { form , dispatch } = this.props; - form.validateFields((err , fieldsValue)=>{ - let { formValue } = this.state.formValue; - formValue = { - ...fieldsValue, - page: 1, - count: 10 - }; - this.setState({formValue}); - - dispatch({ - type: 'systemuser/fetchAll', - payload: { - ...formValue - } - }) - }); - } - - renderQueryForm=()=>{ - - const{ getFieldDecorator } = this.props.form; - - return( - -
- - {getFieldDecorator("loginname")( - - )} - - - {getFieldDecorator("status")( - - )} - - - - -
-
- ) - } - - handlerTableOnChange=(pagination , filters , sorter)=>{ - const { dispatch } = this.props; - let { formValue } = this.state; - - formValue = { - ...formValue, - page: pagination.current, - count: pagination.pageSize, - } - - this.setState({ - formValue:{ - ...formValue, - } - }); - - dispatch({ - type: 'systemuser/fetchAll', - payload:{ - ...formValue, - } - }) - } - - showModal=(currentObj)=>{ - this.setState({ - current: currentObj, - confirmLoading: false, - visible: true, - }) - } - - handlerModalOnOk=(e)=>{ - - e.preventDefault(); - this.setState({ - confirmLoading : true , - }); - const { form } = this.formRef.props; - const { dispatch } = this.props; - form.validateFields((err , fieldsValue)=>{ - if(err) return this.setState({confirmLoading: false}); - - const {current} = this.state; - let currentObj = { - ...current, - ...fieldsValue - }; - dispatch({ - type: 'systemuser/save', - payload:{ - user: currentObj - }, - callback: (result)=>{ - - if(result.success){ - this.setState({ - confirmLoading: false, - visible: false, - }); - dispatch({ - type: 'systemuser/fetchAll', - payload:{ - page: 1, - count: 10 - } - }); - }else { - message.error(result.message); - this.setState({confirmLoading: false}); - } - } + }, }); - }); - } - - handlerDelOnClick=(e , id)=>{ - const { dispatch } = this.props; - dispatch({ - type: 'systemuser/del', - payload:{ - id: id + } else { + message.error(result.message); + this.setState({ confirmLoading: false }); + } + }, + }); + }); + }; + + handlerDelOnClick = (e, id) => { + const { dispatch } = this.props; + dispatch({ + type: 'systemuser/del', + payload: { + id: id, + }, + callback: result => { + if (result.success) { + dispatch({ + type: 'systemuser/fetchAll', + payload: { + ...this.state.formValue, }, - callback:(result)=>{ - if(result.success){ - dispatch({ - type: 'systemuser/fetchAll', - payload:{ - ...this.state.formValue - } - }); - }else { - message.error(result.message); - } - } - }); - } - - render(){ - - const { systemuser:{UserList}, loading } = this.props; - - const columns = [{ - title: '头像', - dataIndex: 'avatar', - },{ - title: '登录名', - dataIndex: 'loginname', - },{ - title: '真实姓名', - dataIndex: 'realname', - },{ - title: 'Email', - dataIndex: 'email', - },{ - title: 'QQ', - dataIndex: 'qq', - },{ - title: '状态', - dataIndex: 'status', - },{ - title: '操作', - render:(record)=>{ - return ( - - {this.showModal(record)}}>编辑 - - {this.handlerDelOnClick(e ,record.id)}}> - 删除 - - - ) - } - }] - - return ( - - - {this.renderQueryForm()} - - - - - - {this.setState({visible: false})}} - onOk={this.handlerModalOnOk} - confirmLoading={this.state.confirmLoading} - destroyOnClose={true} - > - - this.formRef= formRef} current={this.state.current} /> - - - - ) - } + }); + } else { + message.error(result.message); + } + }, + }); + }; + + render() { + const { + systemuser: { UserList }, + loading, + } = this.props; + + const columns = [ + { + title: '头像', + dataIndex: 'avatar', + }, + { + title: '登录名', + dataIndex: 'loginname', + }, + { + title: '真实姓名', + dataIndex: 'realname', + }, + { + title: 'Email', + dataIndex: 'email', + }, + { + title: 'QQ', + dataIndex: 'qq', + }, + { + title: '状态', + dataIndex: 'status', + render: value => {UserStatus[value]}, + }, + { + title: '操作', + render: record => { + return ( + + { + this.showModal(record); + }} + > + 编辑 + + + { + this.handlerDelOnClick(e, record.id); + }} + > + + 删除 + + + + ); + }, + }, + ]; + + return ( + + + {this.renderQueryForm()} + + + + +
+ { + this.setState({ visible: false }); + }} + onOk={this.handlerModalOnOk} + confirmLoading={this.state.confirmLoading} + destroyOnClose={true} + > + (this.formRef = formRef)} + current={this.state.current} + /> + + + + ); + } } - @Form.create() -export class EditUser extends PureComponent{ - - compareToPassword=(rule , value , callback)=>{ - const form = this.props.form; - if (value && value !== form.getFieldValue('password')) { - callback('两次密码输入不一致'); - } else { - callback(); - } +export class EditUser extends PureComponent { + compareToPassword = (rule, value, callback) => { + const form = this.props.form; + if (value && value !== form.getFieldValue('password')) { + callback('两次密码输入不一致'); + } else { + callback(); } - - render(){ - - const { getFieldDecorator } = this.props.form; - const { current } = this.props; - - const flag = (Object.keys(current).length == 0); - - const formItemLayout = { - labelCol:{ - xs: { span : 4}, - sm: { span : 4 }, - }, - wrapperCol:{ - xs: { span : 12}, - sm: { span : 12 }, - } - } - - return( + }; + + render() { + const { getFieldDecorator } = this.props.form; + const { current } = this.props; + + const flag = Object.keys(current).length == 0; + + const formItemLayout = { + labelCol: { + xs: { span: 4 }, + sm: { span: 4 }, + }, + wrapperCol: { + xs: { span: 12 }, + sm: { span: 12 }, + }, + }; + + return ( +
+
+ + {getFieldDecorator('loginname', { + initialValue: current.loginname, + rules: [ + { + required: true, + message: '请输入登录名', + }, + ], + })()} + + {flag ? (
- - - {getFieldDecorator('loginname',{ - initialValue: current.loginname, - rules:[ - { - required: true, - message: '请输入登录名', - } - ] - })( - - )} - + + {getFieldDecorator('password', { + rules: [ + { + required: true, + message: '请输入密码', + }, + ], + })()} + + + {getFieldDecorator('repassword', { + rules: [ { - flag ? -
- - {getFieldDecorator("password",{ - rules:[ - { - required: true, - message: '请输入密码', - } - ] - })( - - )} - - - {getFieldDecorator("repassword",{ - rules:[ - { - required: true, - message: '请输入密码', - },{ - validator:this.compareToPassword, - } - ] - })( - - )} - -
- : - null - } - - {getFieldDecorator("realname",{ - initialValue: current.realname, - })( - - )} - - - {getFieldDecorator("email",{ - initialValue: current.email, - })( - - )} - - - {getFieldDecorator("qq",{ - initialValue: current.qq, - })( - - )} - - + required: true, + message: '请输入密码', + }, + { + validator: this.compareToPassword, + }, + ], + })()} +
- ) - } -} \ No newline at end of file + ) : null} + + {getFieldDecorator('realname', { + initialValue: current.realname, + })()} + + + {getFieldDecorator('email', { + initialValue: current.email, + })()} + + + {getFieldDecorator('qq', { + initialValue: current.qq, + })()} + + +
+ ); + } +} diff --git a/antdsp-core/.gitignore b/antdsp-core/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/antdsp-core/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/antdsp-core/src/main/java/com/antdsp/data/entityeenum/UserStatus.java b/antdsp-core/src/main/java/com/antdsp/data/entityeenum/UserStatus.java index ab9fab3..98dfada 100644 --- a/antdsp-core/src/main/java/com/antdsp/data/entityeenum/UserStatus.java +++ b/antdsp-core/src/main/java/com/antdsp/data/entityeenum/UserStatus.java @@ -2,6 +2,7 @@ package com.antdsp.data.entityeenum; public enum UserStatus { NORMAL("正常"), + DISABLED("已禁用"), FORBIDDEN("异常"); private String value; -- Gitee From eeaeb41ff694ad30617207d16a802a4a2051228a Mon Sep 17 00:00:00 2001 From: lijiantao Date: Thu, 13 Jun 2019 17:55:53 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/CorsConfiguration.java | 30 ++++ .../antdsp/common/shiro/AntdspShiroRealm.java | 2 +- .../main/java/com/antdsp/web/rest/Login.java | 6 +- antdsp-admin/src/main/web/package.json | 7 +- antdsp-admin/src/main/web/src/models/login.js | 27 ++-- .../src/main/web/src/pages/Login/Index.js | 138 +++++++++++------- .../src/main/web/src/pages/Login/style.less | 21 +++ .../src/main/web/src/services/login.js | 13 ++ .../src/main/web/src/utils/authority.js | 17 ++- .../src/main/web/src/utils/request.js | 11 +- 10 files changed, 187 insertions(+), 85 deletions(-) create mode 100644 antdsp-admin/src/main/java/com/antdsp/common/configuration/CorsConfiguration.java create mode 100644 antdsp-admin/src/main/web/src/pages/Login/style.less create mode 100644 antdsp-admin/src/main/web/src/services/login.js diff --git a/antdsp-admin/src/main/java/com/antdsp/common/configuration/CorsConfiguration.java b/antdsp-admin/src/main/java/com/antdsp/common/configuration/CorsConfiguration.java new file mode 100644 index 0000000..b0acba4 --- /dev/null +++ b/antdsp-admin/src/main/java/com/antdsp/common/configuration/CorsConfiguration.java @@ -0,0 +1,30 @@ +package com.antdsp.common.configuration; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * + *

title:CorsConfiguration

+ *

Description: 跨域访问

+ *

Copyright: Copyright (c) 2019

+ * + * @author lijiantao + * @date 2019年6月13日 + * @email a496401006@qq.com + * + */ +@Configuration +public class CorsConfiguration implements WebMvcConfigurer{ + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") + .allowCredentials(true) + .allowedMethods("GET", "POST", "DELETE", "PUT") + .maxAge(3600);; + } + +} diff --git a/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java b/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java index ec9d486..3271f34 100644 --- a/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java +++ b/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java @@ -39,7 +39,7 @@ public class AntdspShiroRealm extends AuthorizingRealm{ if(user == null) { throw new UnknownAccountException("用户名/密码错误"); } - if(user.getPassword().equals(password)) { + if(!user.getPassword().equals(password)) { throw new UnknownAccountException("用户名/密码错误"); } if(UserStatus.DISABLED.equals(user.getStatus())) { diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java index 6892afb..e236338 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java @@ -5,13 +5,12 @@ import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.session.Session; import org.apache.shiro.subject.Subject; -import org.springframework.util.StringUtils; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; import com.antdsp.common.AntdspResponse; import com.antdsp.common.ResponseCode; @@ -23,6 +22,7 @@ public class Login { @GetMapping("/login") public AntdspResponse login() { return AntdspResponse.error(ResponseCode.UNAUTHORIZED , "请先登录"); + // throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "请先登录"); } @PostMapping("/login") diff --git a/antdsp-admin/src/main/web/package.json b/antdsp-admin/src/main/web/package.json index 8b71bcb..60cbd32 100644 --- a/antdsp-admin/src/main/web/package.json +++ b/antdsp-admin/src/main/web/package.json @@ -32,11 +32,6 @@ "tslint": "npm run tslint:fix", "tslint:fix": "tslint --fix 'src/**/*.ts*'" }, - "husky": { - "hooks": { - "pre-commit": "npm run lint-staged" - } - }, "lint-staged": { "**/*.less": "stylelint --syntax less", "**/*.{js,jsx}": "npm run lint-staged:js", @@ -53,6 +48,7 @@ "dependencies": { "@antv/data-set": "^0.10.1", "antd": "^3.16.1", + "axios": "^0.19.0", "bizcharts": "^3.4.3", "bizcharts-plugin-slider": "^2.1.1-beta.1", "classnames": "^2.2.6", @@ -60,6 +56,7 @@ "enquire-js": "^0.2.1", "express": "^4.16.4", "gg-editor": "^2.0.2", + "js-md5": "^0.7.3", "lodash": "^4.17.11", "lodash-decorators": "^6.0.1", "memoize-one": "^5.0.0", diff --git a/antdsp-admin/src/main/web/src/models/login.js b/antdsp-admin/src/main/web/src/models/login.js index b5d226a..fb62998 100644 --- a/antdsp-admin/src/main/web/src/models/login.js +++ b/antdsp-admin/src/main/web/src/models/login.js @@ -4,6 +4,7 @@ import { fakeAccountLogin, getFakeCaptcha } from '@/services/api'; import { setAuthority } from '@/utils/authority'; import { getPageQuery } from '@/utils/utils'; import { reloadAuthorized } from '@/utils/Authorized'; +import { loginAction } from '@/services/login' export default { namespace: 'login', @@ -13,14 +14,15 @@ export default { }, effects: { - *login({ payload }, { call, put }) { - const response = yield call(fakeAccountLogin, payload); - yield put({ - type: 'changeLoginStatus', - payload: response, - }); - // Login successfully - if (response.status === 'ok') { + *login({ payload ,callback}, { call, put }) { + const response = yield call(loginAction, payload); + + if(response.success){ + yield put({ + type: 'changeLoginStatus', + payload: { authority: 'admin', sessionId: response.message }, + }); + reloadAuthorized(); const urlParams = new URL(window.location.href); const params = getPageQuery(); @@ -37,6 +39,8 @@ export default { } } yield put(routerRedux.replace(redirect || '/')); + }else { + if(callback) callback(response); } }, @@ -47,10 +51,7 @@ export default { *logout(_, { put }) { yield put({ type: 'changeLoginStatus', - payload: { - status: false, - currentAuthority: 'guest', - }, + payload: { authority: 'guest', sessionId: null}, }); reloadAuthorized(); // redirect @@ -69,7 +70,7 @@ export default { reducers: { changeLoginStatus(state, { payload }) { - setAuthority(payload.currentAuthority); + setAuthority(payload.authority); return { ...state, status: payload.status, diff --git a/antdsp-admin/src/main/web/src/pages/Login/Index.js b/antdsp-admin/src/main/web/src/pages/Login/Index.js index 777dc84..af69fe2 100644 --- a/antdsp-admin/src/main/web/src/pages/Login/Index.js +++ b/antdsp-admin/src/main/web/src/pages/Login/Index.js @@ -1,60 +1,90 @@ import { PureComponent } from 'react'; -import { Card, Form, Input, Icon } from 'antd'; +import { connect } from 'dva'; +import { Card, Form, Input, Icon, Button, message } from 'antd'; import Block from '@/custom/Block'; +import styles from './style.less'; +import md5 from 'js-md5' const FormItem = Form.Item; +@connect(({ login })=>({ + login +})) @Form.create() -export default class Login extends PureComponent { - render() { - const { - form: { getFieldDecorator }, - } = this.props; - - return ( -
- {/*
*/} -
- Antdsp Login Page -
-
-
- - {getFieldDecorator('loginname', { - rules: [ - { - required: true, - message: '', - }, - ], - })( - } - placeholder="用户名" - /> - )} - - - {getFieldDecorator('password', { - rules: [ - { - required: true, - message: '', - }, - ], - })( - } - type="password" - placeholder="密码∂" - /> - )} - - -
- - {/*
*/} -
- ); - } -} +export default class Login extends PureComponent{ + + state={ + btnLoading: false + } + + handlerLogin=()=>{ + const { form, dispatch } = this.props; + + this.setState({btnLoading: true}); + + form.validateFields((err , fieldsValue)=>{ + if(err) return this.setState({btnLoading: false}); + + const loginname = fieldsValue.loginname; + let password = fieldsValue.password; + dispatch({ + type: 'login/login', + payload:{ + loginname: loginname, + password: md5(password) + }, + callback:(result)=>{ + message.error(result.message); + this.setState({btnLoading: false}); + } + }) + + }); + } + + render(){ + + const { + form: { getFieldDecorator }, + } = this.props; + + return( +
+
+
+
Antdsp Login Page
+
+
+ + {getFieldDecorator('loginname',{ + initialValue: 'jt-lee', + rules: [{ + required: true, + message: '请输入登录名', + }] + })( + } placeholder="用户名" /> + )} + + + {getFieldDecorator('password',{ + initialValue: '123456', + rules: [{ + required: true, + message: '请输入密码', + }] + })( + } type="password" placeholder="密 码" /> + )} + + + + + +
+
+
+
+ ) + } +} \ No newline at end of file diff --git a/antdsp-admin/src/main/web/src/pages/Login/style.less b/antdsp-admin/src/main/web/src/pages/Login/style.less new file mode 100644 index 0000000..fa972d7 --- /dev/null +++ b/antdsp-admin/src/main/web/src/pages/Login/style.less @@ -0,0 +1,21 @@ +@import '~antd/lib/style/themes/default.less'; + +.container{ + display: flex; + height: 100%; + flex-direction: column; + background-color: #20815d; +} + +.main{ + width: 360px; + color: #fff; + margin: 0 auto; + margin-top: 150px; + .title{ + text-align: center; + font-size: 32px; + font-weight: bold; + margin-bottom: 20px; + } +} \ No newline at end of file diff --git a/antdsp-admin/src/main/web/src/services/login.js b/antdsp-admin/src/main/web/src/services/login.js new file mode 100644 index 0000000..86ccf9b --- /dev/null +++ b/antdsp-admin/src/main/web/src/services/login.js @@ -0,0 +1,13 @@ +import { stringify } from 'qs'; +import request from '@/utils/request'; + +const API_BASE = "/antdsp-api"; + +export async function loginAction(param){ + return request(`${API_BASE}/login`,{ + method: 'POST', + data: { + ...param + } + }) +} \ No newline at end of file diff --git a/antdsp-admin/src/main/web/src/utils/authority.js b/antdsp-admin/src/main/web/src/utils/authority.js index bb1aa23..bfa07ba 100644 --- a/antdsp-admin/src/main/web/src/utils/authority.js +++ b/antdsp-admin/src/main/web/src/utils/authority.js @@ -2,7 +2,7 @@ export function getAuthority(str) { // return localStorage.getItem('antd-pro-authority') || ['admin', 'user']; const authorityString = - typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str; + typeof str === 'undefined' ? localStorage.getItem('antdsp-authority') : str; // authorityString could be admin, "admin", ["admin"] let authority; try { @@ -13,13 +13,14 @@ export function getAuthority(str) { if (typeof authority === 'string') { return [authority]; } - // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。 - if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') { - return ['admin']; - } - return authority; + return authority || ['guest']; } -export function setAuthority(authority) { +export function setAuthority(authority,sessionId) { const proAuthority = typeof authority === 'string' ? [authority] : authority; - return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority)); + if(sessionId) localStorage.setItem('Authorization', sessionId); + return localStorage.setItem('antdsp-authority', JSON.stringify(proAuthority)); +} + +export function getAuthorization() { + return localStorage.getItem('Authorization'); } diff --git a/antdsp-admin/src/main/web/src/utils/request.js b/antdsp-admin/src/main/web/src/utils/request.js index 0801b1d..936e3db 100644 --- a/antdsp-admin/src/main/web/src/utils/request.js +++ b/antdsp-admin/src/main/web/src/utils/request.js @@ -2,7 +2,7 @@ * request 网络请求工具 * 更详细的api文档: https://bigfish.alipay.com/doc/api#request */ -import { extend } from 'umi-request'; +import { extend , fetch } from 'umi-request'; import { notification } from 'antd'; import router from 'umi/router'; @@ -69,4 +69,13 @@ const request = extend({ credentials: 'include', // 默认请求是否带上cookie }); +//response 的拦截器, response 为一个Response 对象 , options 为request 参数 +fetch.interceptors.response.use(function (response , options) { + response((resolve , reject)=>{ + console.log("AAA"); + }) + console.log(); + return response; +}); + export default request; -- Gitee From 3dfe108fc87be752bf7cd5016a6ed1d078ca5a49 Mon Sep 17 00:00:00 2001 From: lijiantao Date: Fri, 14 Jun 2019 18:01:53 +0800 Subject: [PATCH 3/5] tijiao --- .../configuration/ShiroConfiguration.java | 9 +++ .../shiro/AntdspShiroSessionManager.java | 40 +++++++++++ .../main/java/com/antdsp/web/rest/Login.java | 2 +- .../antdsp/web/rest/sys/SystemMenuApi.java | 37 ++++++++++ .../antdsp/web/rest/sys/SystemRoleApi.java | 7 +- .../antdsp/web/rest/sys/SystemUserApi.java | 10 +++ .../components/GlobalHeader/RightContent.js | 68 +------------------ .../web/src/components/SelectLang/index.js | 10 +-- .../src/main/web/src/locales/zh-CN/menu.js | 4 +- antdsp-admin/src/main/web/src/models/login.js | 9 ++- .../main/web/src/pages/System/User/Index.js | 49 +++++++++++-- .../web/src/pages/System/User/models/user.js | 8 ++- .../src/main/web/src/services/system.js | 4 ++ .../src/main/web/src/services/user.js | 2 +- .../src/main/web/src/utils/authority.js | 7 +- .../src/main/web/src/utils/request.js | 40 +++++++++-- .../antdsp/dao/jpa/AntdspBaseRepository.java | 34 +++++++++- .../dao/jpa/AntdspBaseRepositoryImpl.java | 26 ++++++- .../com/antdsp/dao/jpa/system/MenuJpa.java | 5 +- .../com/antdsp/dao/jpa/system/RoleJpa.java | 6 ++ 20 files changed, 278 insertions(+), 99 deletions(-) create mode 100644 antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroSessionManager.java diff --git a/antdsp-admin/src/main/java/com/antdsp/common/configuration/ShiroConfiguration.java b/antdsp-admin/src/main/java/com/antdsp/common/configuration/ShiroConfiguration.java index 7658d6b..09fa7ef 100644 --- a/antdsp-admin/src/main/java/com/antdsp/common/configuration/ShiroConfiguration.java +++ b/antdsp-admin/src/main/java/com/antdsp/common/configuration/ShiroConfiguration.java @@ -5,6 +5,7 @@ import java.util.Map; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.realm.Realm; +import org.apache.shiro.session.mgt.SessionManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; @@ -12,6 +13,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.antdsp.common.shiro.AntdspShiroRealm; +import com.antdsp.common.shiro.AntdspShiroSessionManager; /** * @@ -37,9 +39,16 @@ public class ShiroConfiguration { public SecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(shiroRealm()); + securityManager.setSessionManager(shiroSessionManager()); return securityManager; } + @Bean + public SessionManager shiroSessionManager() { + AntdspShiroSessionManager sessionManger = new AntdspShiroSessionManager(); + return sessionManger; + } + @Bean public ShiroFilterFactoryBean shirofilterFactoryBean(SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean(); diff --git a/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroSessionManager.java b/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroSessionManager.java new file mode 100644 index 0000000..201e5ea --- /dev/null +++ b/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroSessionManager.java @@ -0,0 +1,40 @@ +package com.antdsp.common.shiro; + +import java.io.Serializable; + +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; + +import org.apache.shiro.web.servlet.ShiroHttpServletRequest; +import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; +import org.apache.shiro.web.util.WebUtils; +import org.springframework.util.StringUtils; + +public class AntdspShiroSessionManager extends DefaultWebSessionManager{ + + private static final String AUTHORIZATION = "Authorization"; + private static final String REFERENCED_SESSION_ID_SOURCE = "Stateless request"; + + public AntdspShiroSessionManager() { + super(); + } + + @Override + protected Serializable getSessionId(ServletRequest request, ServletResponse response) { + // TODO Auto-generated method stub + HttpServletRequest httpRequest = WebUtils.toHttp(request); + + String sessionId = httpRequest.getHeader(AUTHORIZATION); + if(!StringUtils.isEmpty(sessionId)) { + request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, REFERENCED_SESSION_ID_SOURCE); + request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, sessionId); + request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID , true); + + return sessionId; + }else { + return super.getSessionId(request, response); + } + } + +} diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java index e236338..cd50c6c 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java @@ -22,7 +22,6 @@ public class Login { @GetMapping("/login") public AntdspResponse login() { return AntdspResponse.error(ResponseCode.UNAUTHORIZED , "请先登录"); - // throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "请先登录"); } @PostMapping("/login") @@ -32,6 +31,7 @@ public class Login { try { subject.login(token); Session session = subject.getSession(); + session.setTimeout(180000); return AntdspResponse.success(session.getId().toString()); }catch(AuthenticationException e) { return AntdspResponse.error(e.getMessage()); diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemMenuApi.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemMenuApi.java index fb45c00..82f2fec 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemMenuApi.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemMenuApi.java @@ -1,9 +1,19 @@ package com.antdsp.web.rest.sys; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.jpa.domain.Specification; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -16,6 +26,7 @@ import org.springframework.web.bind.annotation.RestController; import com.antdsp.common.AntdspResponse; import com.antdsp.dao.jpa.system.MenuJpa; +import com.antdsp.data.entity.User; import com.antdsp.data.entity.system.SystemMenu; import com.antdsp.data.entityeenum.MenuType; import com.antdsp.web.dto.MenuTree; @@ -80,6 +91,32 @@ public class SystemMenuApi { return AntdspResponse.success(); } + /** + * 获取当前登录人的路由信息 + * @return + */ + @GetMapping("/route") + public MenuTree queryRouteMenu() { + + Subject subject = SecurityUtils.getSubject(); + User current = (User) subject.getPrincipal(); + + String queryStr = "SELECT sm.* FROM tb_system_menu sm ,tb_role_user ru, tb_role_menu rm "; + queryStr += " WHERE ru.user_id =:userId AND ru.role_id = rm.role_id AND rm.menu_id = sm.id AND sm.menu_type = 'MENU' "; + + Map params = new HashMap<>(); + params.put("userId", 2L); + + List allMenus = this.menuJpa.queryBySQL(queryStr, params, SystemMenu.class); + + MenuTree rootMenu = new MenuTree(); + rootMenu.setId(0L); + rootMenu.setName("antdsp"); + rootMenu.setPath("/"); + this.child(rootMenu, allMenus); + return rootMenu; + + } private void dtoToData(MenuTree menu , SystemMenu systemenu) { diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemRoleApi.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemRoleApi.java index be389d7..2792df7 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemRoleApi.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemRoleApi.java @@ -92,7 +92,7 @@ public class SystemRoleApi { Map params = new HashMap<>(); params.put("roleId", id); - List list = this.roleJpa.queryAllBySQL(sqlStr, params); + List list = this.roleJpa.queryBySQL(sqlStr, params , null); List menuIds = new ArrayList<>(); for(BigInteger item : list) { menuIds.add(item.longValue()); @@ -157,6 +157,11 @@ public class SystemRoleApi { return AntdspResponse.success(); } + @GetMapping("queryRoleNameAndIds") + public List queryRoleNameAndIds(){ + return this.roleJpa.findRoleNameAndIds(); + } + private int saveRoleMenu(List menuIds , Long roleId) { if(menuIds != null &&menuIds.size() > 0) { StringBuffer sqlStr = new StringBuffer("INSERT INTO `tb_role_menu` (`role_id`,`menu_id`) VALUES "); diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java index 95054ff..17899ac 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java @@ -10,6 +10,8 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -108,4 +110,12 @@ public class SystemUserApi { this.userJpa.delete(oldUser); return AntdspResponse.success(); } + + @GetMapping("/current") + public User current() { + Subject subject = SecurityUtils.getSubject(); + User current = (User) subject.getPrincipal(); + current.setPassword(""); + return current; + } } diff --git a/antdsp-admin/src/main/web/src/components/GlobalHeader/RightContent.js b/antdsp-admin/src/main/web/src/components/GlobalHeader/RightContent.js index d40e116..3755c03 100644 --- a/antdsp-admin/src/main/web/src/components/GlobalHeader/RightContent.js +++ b/antdsp-admin/src/main/web/src/components/GlobalHeader/RightContent.js @@ -78,14 +78,6 @@ export default class GlobalHeaderRight extends PureComponent { - - - - - - - - @@ -116,63 +108,7 @@ export default class GlobalHeaderRight extends PureComponent { console.log('enter', value); // eslint-disable-line }} /> - - - - - - { - console.log(item, tabProps); // eslint-disable-line - this.changeReadState(item, tabProps); - }} - loading={fetchingNotices} - locale={{ - emptyText: formatMessage({ id: 'component.noticeIcon.empty' }), - clear: formatMessage({ id: 'component.noticeIcon.clear' }), - viewMore: formatMessage({ id: 'component.noticeIcon.view-more' }), - notification: formatMessage({ id: 'component.globalHeader.notification' }), - message: formatMessage({ id: 'component.globalHeader.message' }), - event: formatMessage({ id: 'component.globalHeader.event' }), - }} - onClear={onNoticeClear} - onPopupVisibleChange={onNoticeVisibleChange} - onViewMore={() => message.info('Click on view more')} - clearClose - > - - - - - {currentUser.name ? ( + {currentUser.realname ? ( - {currentUser.name} + {currentUser.realname} ) : ( diff --git a/antdsp-admin/src/main/web/src/components/SelectLang/index.js b/antdsp-admin/src/main/web/src/components/SelectLang/index.js index 3ce3fb8..0151a98 100644 --- a/antdsp-admin/src/main/web/src/components/SelectLang/index.js +++ b/antdsp-admin/src/main/web/src/components/SelectLang/index.js @@ -13,18 +13,18 @@ export default class SelectLang extends PureComponent { render() { const { className } = this.props; const selectedLang = getLocale(); - const locales = ['zh-CN', 'zh-TW', 'en-US', 'pt-BR']; + const locales = ['zh-CN', 'en-US']; const languageLabels = { 'zh-CN': '简体中文', - 'zh-TW': '繁体中文', + // 'zh-TW': '繁体中文', 'en-US': 'English', - 'pt-BR': 'Português', + // 'pt-BR': 'Português', }; const languageIcons = { 'zh-CN': '🇨🇳', - 'zh-TW': '🇭🇰', + // 'zh-TW': '🇭🇰', 'en-US': '🇬🇧', - 'pt-BR': '🇧🇷', + // 'pt-BR': '🇧🇷', }; const langMenu = ( diff --git a/antdsp-admin/src/main/web/src/locales/zh-CN/menu.js b/antdsp-admin/src/main/web/src/locales/zh-CN/menu.js index 3cb2b06..ef57461 100644 --- a/antdsp-admin/src/main/web/src/locales/zh-CN/menu.js +++ b/antdsp-admin/src/main/web/src/locales/zh-CN/menu.js @@ -3,5 +3,7 @@ export default { 'menu.system': '系统管理', 'menu.system.user': '用户管理', 'menu.system.menu': '菜单管理', - 'menu.system.role': '角色管理' + 'menu.system.role': '角色管理', + 'menu.account.center': '个人中心', + 'menu.account.logout': '退出登录', }; diff --git a/antdsp-admin/src/main/web/src/models/login.js b/antdsp-admin/src/main/web/src/models/login.js index fb62998..396b317 100644 --- a/antdsp-admin/src/main/web/src/models/login.js +++ b/antdsp-admin/src/main/web/src/models/login.js @@ -34,6 +34,9 @@ export default { if (redirect.match(/^\/.*#/)) { redirect = redirect.substr(redirect.indexOf('#') + 1); } + if(redirect.indexOf('/antdsp') == 0){ + redirect = redirect.substr(7); + } } else { redirect = null; } @@ -55,10 +58,10 @@ export default { }); reloadAuthorized(); // redirect - if (window.location.pathname !== '/user/login') { + if (window.location.pathname !== '/login') { yield put( routerRedux.replace({ - pathname: '/user/login', + pathname: '/login', search: stringify({ redirect: window.location.href, }), @@ -70,7 +73,7 @@ export default { reducers: { changeLoginStatus(state, { payload }) { - setAuthority(payload.authority); + setAuthority(payload.authority , payload.sessionId); return { ...state, status: payload.status, diff --git a/antdsp-admin/src/main/web/src/pages/System/User/Index.js b/antdsp-admin/src/main/web/src/pages/System/User/Index.js index e82eadf..5eba6ef 100644 --- a/antdsp-admin/src/main/web/src/pages/System/User/Index.js +++ b/antdsp-admin/src/main/web/src/pages/System/User/Index.js @@ -13,7 +13,9 @@ import { Modal, message, Popconfirm, + Checkbox, } from 'antd'; +import md5 from 'js-md5' const FormItem = Form.Item; @@ -121,10 +123,19 @@ export default class extends PureComponent { }; showModal = currentObj => { - this.setState({ - current: currentObj, - confirmLoading: false, - visible: true, + + const { dispatch } = this.props; + + dispatch({ + type: 'systemuser/fetchRoles', + callback:(result)=>{ + this.setState({ + current: currentObj, + confirmLoading: false, + visible: true, + roles: result + }); + } }); }; @@ -139,6 +150,11 @@ export default class extends PureComponent { if (err) return this.setState({ confirmLoading: false }); const { current } = this.state; + let password = fieldsValue.password; + if(password){ + fieldsValue.password = md5(password); + } + let currentObj = { ...current, ...fieldsValue, @@ -293,6 +309,7 @@ export default class extends PureComponent { (this.formRef = formRef)} current={this.state.current} + roles={this.state.roles} /> @@ -312,9 +329,23 @@ export class EditUser extends PureComponent { } }; + roleToNameId(roleNameId){ + + let newRoleNameId=[]; + + roleNameId.map((item=>{ + let nameid = { + label: item.roleName, + value: item.id, + } + newRoleNameId.push(nameid); + })); + return newRoleNameId; +} + render() { const { getFieldDecorator } = this.props.form; - const { current } = this.props; + const { current, roles } = this.props; const flag = Object.keys(current).length == 0; @@ -385,6 +416,14 @@ export class EditUser extends PureComponent { initialValue: current.qq, })()} + + { + getFieldDecorator('roles',{ + })( + + ) + } + ); diff --git a/antdsp-admin/src/main/web/src/pages/System/User/models/user.js b/antdsp-admin/src/main/web/src/pages/System/User/models/user.js index 3460b77..e7c3a27 100644 --- a/antdsp-admin/src/main/web/src/pages/System/User/models/user.js +++ b/antdsp-admin/src/main/web/src/pages/System/User/models/user.js @@ -2,7 +2,8 @@ import { fetchAllUser, addUser, updateUser, - delUserById + delUserById, + queryRoles } from '@/services/system'; export default { @@ -36,6 +37,11 @@ export default { *del({payload , callback},{call}){ const response = yield call(delUserById , payload); if(callback) callback(response); + }, + *fetchRoles({callback}, {call}){ + const response = yield call(queryRoles); + console.log(response); + if(callback) callback(response); } }, diff --git a/antdsp-admin/src/main/web/src/services/system.js b/antdsp-admin/src/main/web/src/services/system.js index f4f4fee..571166d 100644 --- a/antdsp-admin/src/main/web/src/services/system.js +++ b/antdsp-admin/src/main/web/src/services/system.js @@ -93,4 +93,8 @@ export async function delRoleById(param){ export async function fetchRoleById(param){ return request(`${ROLE_API}/${param.id}`); +} + +export async function queryRoles(){ + return request(`${ROLE_API}/queryRoleNameAndIds`); } \ No newline at end of file diff --git a/antdsp-admin/src/main/web/src/services/user.js b/antdsp-admin/src/main/web/src/services/user.js index 89e03c6..bc16023 100644 --- a/antdsp-admin/src/main/web/src/services/user.js +++ b/antdsp-admin/src/main/web/src/services/user.js @@ -5,5 +5,5 @@ export async function query() { } export async function queryCurrent() { - return request('/api/currentUser'); + return request('/antdsp-api/system/user/current'); } diff --git a/antdsp-admin/src/main/web/src/utils/authority.js b/antdsp-admin/src/main/web/src/utils/authority.js index bfa07ba..2cb59f4 100644 --- a/antdsp-admin/src/main/web/src/utils/authority.js +++ b/antdsp-admin/src/main/web/src/utils/authority.js @@ -1,3 +1,5 @@ +import { setAuthorization } from './request'; + // use localStorage to store the authority info, which might be sent from server in actual project. export function getAuthority(str) { // return localStorage.getItem('antd-pro-authority') || ['admin', 'user']; @@ -17,10 +19,7 @@ export function getAuthority(str) { } export function setAuthority(authority,sessionId) { const proAuthority = typeof authority === 'string' ? [authority] : authority; - if(sessionId) localStorage.setItem('Authorization', sessionId); + if(sessionId) setAuthorization(sessionId); return localStorage.setItem('antdsp-authority', JSON.stringify(proAuthority)); } -export function getAuthorization() { - return localStorage.getItem('Authorization'); -} diff --git a/antdsp-admin/src/main/web/src/utils/request.js b/antdsp-admin/src/main/web/src/utils/request.js index 936e3db..67cabd4 100644 --- a/antdsp-admin/src/main/web/src/utils/request.js +++ b/antdsp-admin/src/main/web/src/utils/request.js @@ -6,6 +6,8 @@ import { extend , fetch } from 'umi-request'; import { notification } from 'antd'; import router from 'umi/router'; +var Authorization = ""; + const codeMessage = { 200: '服务器成功返回请求的数据。', 201: '新建或修改数据成功。', @@ -67,15 +69,43 @@ const errorHandler = error => { const request = extend({ errorHandler, // 默认错误处理 credentials: 'include', // 默认请求是否带上cookie + headers:{ + Authorization: Authorization, + } }); //response 的拦截器, response 为一个Response 对象 , options 为request 参数 -fetch.interceptors.response.use(function (response , options) { - response((resolve , reject)=>{ - console.log("AAA"); - }) - console.log(); +fetch.interceptors.response.use(async function (response , options) { + const result = await response.clone().json(); + + if(typeof result.success === 'undefined'){ + return response; + }else if(result.success){ + return response; + }else { + customErrorHandler(result); + } return response; }); +const customErrorHandler = error =>{ + const { code , message } = error; + if(code == 1401){ + notification.error({ + message: '未登录或登录已过期,请重新登录。', + }); + window.g_app._store.dispatch({ + type: 'login/logout', + }); + return; + } + notification.error({ + message: `请求错误 ${code}: ${message}`, + }); +} + +export function setAuthorization(authorization){ + Authorization = authorization; +} + export default request; diff --git a/antdsp-core/src/main/java/com/antdsp/dao/jpa/AntdspBaseRepository.java b/antdsp-core/src/main/java/com/antdsp/dao/jpa/AntdspBaseRepository.java index f2d5183..fa99ea3 100644 --- a/antdsp-core/src/main/java/com/antdsp/dao/jpa/AntdspBaseRepository.java +++ b/antdsp-core/src/main/java/com/antdsp/dao/jpa/AntdspBaseRepository.java @@ -24,7 +24,39 @@ import org.springframework.data.repository.NoRepositoryBean; @NoRepositoryBean public interface AntdspBaseRepository extends JpaRepository , JpaSpecificationExecutor{ + /** + * 执行SQL语句(update, insert, delete),表名、字段名和数据库对应 + * @param sqlString SQL语句 + * @param params 参数 + * @return + */ int executeSQL(String sqlString , Map params); - List queryAllBySQL(String sqlStr , Map params); + /** + * 一般的SQL语句(select),表名、字段名和数据库对应 + * @param sqlString + * @param params + * @return + */ + List queryBySQL(String sqlStr , Map params, Class clazz); + + /** + * (select) 多表联合查询,表名、字段名和实体类对应 + * @param sqlStr + * @param params + * @return + */ + List selectUnion(String queryStr , Map params , Class clazz); + + /** + * 分页(select) 多表联合查询,表名、字段名和实体类对应 + * @param sqlStr + * @param params + * @param clazz + * @param start + * @param count + * @return + */ + List selectUnion(String queryStr , Map params , Class clazz, int start, int count); + } diff --git a/antdsp-core/src/main/java/com/antdsp/dao/jpa/AntdspBaseRepositoryImpl.java b/antdsp-core/src/main/java/com/antdsp/dao/jpa/AntdspBaseRepositoryImpl.java index 57760e4..2ca6fcf 100644 --- a/antdsp-core/src/main/java/com/antdsp/dao/jpa/AntdspBaseRepositoryImpl.java +++ b/antdsp-core/src/main/java/com/antdsp/dao/jpa/AntdspBaseRepositoryImpl.java @@ -7,6 +7,7 @@ import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.Query; +import javax.persistence.TypedQuery; import org.springframework.data.jpa.repository.support.SimpleJpaRepository; @@ -54,11 +55,32 @@ public class AntdspBaseRepositoryImpl extends Simple } @Override - public List queryAllBySQL(String sqlStr, Map params) { + public List selectUnion(String queryStr, Map params, Class clazz) { - Query query = em.createNativeQuery(sqlStr); + TypedQuery query = em.createQuery(queryStr, clazz); this.querySetParameter(query, params); return query.getResultList(); } + @Override + public List selectUnion(String queryStr, Map params, Class clazz, int start, int count) { + + TypedQuery query = em.createQuery(queryStr, clazz); + this.querySetParameter(query, params); + query.setFirstResult(start).setMaxResults(count); + return query.getResultList(); + } + + @Override + public List queryBySQL(String sqlStr, Map params, Class clazz) { + + Query query; + if(clazz == null) { + query = em.createNativeQuery(sqlStr); + }else { + query = em.createNativeQuery(sqlStr, clazz); + } + this.querySetParameter(query, params); + return query.getResultList(); + } } diff --git a/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/MenuJpa.java b/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/MenuJpa.java index 6bb2c54..b83e48a 100644 --- a/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/MenuJpa.java +++ b/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/MenuJpa.java @@ -1,12 +1,11 @@ package com.antdsp.dao.jpa.system; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Repository; +import com.antdsp.dao.jpa.AntdspBaseRepository; import com.antdsp.data.entity.system.SystemMenu; @Repository("menuJpa") -public interface MenuJpa extends JpaRepository , JpaSpecificationExecutor{ +public interface MenuJpa extends AntdspBaseRepository{ } diff --git a/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/RoleJpa.java b/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/RoleJpa.java index b7e3ed7..32c159f 100644 --- a/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/RoleJpa.java +++ b/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/RoleJpa.java @@ -1,5 +1,8 @@ package com.antdsp.dao.jpa.system; +import java.util.List; + +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import com.antdsp.dao.jpa.AntdspBaseRepository; @@ -7,5 +10,8 @@ import com.antdsp.data.entity.system.SystemRole; @Repository("roleJpa") public interface RoleJpa extends AntdspBaseRepository{ + + @Query("FROM SystemRole s ORDER BY s.created") + List findRoleNameAndIds(); } -- Gitee From 2c0a4f47a2ad015756d938cf7fbe3e0697527c21 Mon Sep 17 00:00:00 2001 From: "a496401006@qq.com" Date: Sun, 16 Jun 2019 16:32:24 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/antdsp/web/dto/SystemUserDto.java | 40 ++++++++++ .../antdsp/web/rest/sys/SystemUserApi.java | 77 +++++++++++++++++-- .../main/web/src/pages/System/User/Index.js | 47 +++++------ .../web/src/pages/System/User/models/user.js | 31 +++++++- .../src/main/web/src/services/system.js | 9 ++- .../main/java/com/antdsp/dao/jpa/UserJpa.java | 4 +- 6 files changed, 174 insertions(+), 34 deletions(-) create mode 100644 antdsp-admin/src/main/java/com/antdsp/web/dto/SystemUserDto.java diff --git a/antdsp-admin/src/main/java/com/antdsp/web/dto/SystemUserDto.java b/antdsp-admin/src/main/java/com/antdsp/web/dto/SystemUserDto.java new file mode 100644 index 0000000..f6b0a53 --- /dev/null +++ b/antdsp-admin/src/main/java/com/antdsp/web/dto/SystemUserDto.java @@ -0,0 +1,40 @@ +package com.antdsp.web.dto; + +import java.util.List; + +import com.antdsp.data.entity.User; +import com.antdsp.data.entity.system.SystemRole; + +public class SystemUserDto { + private User user; + + private List roleIds; + + private List roles; + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public List getRoleIds() { + return roleIds; + } + + public void setRoleIds(List roleIds) { + this.roleIds = roleIds; + } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + +} diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java index 17899ac..819d1c1 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java @@ -1,5 +1,6 @@ package com.antdsp.web.rest.sys; +import java.math.BigInteger; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -33,8 +34,11 @@ import org.springframework.web.bind.annotation.RestController; import com.antdsp.common.AntdspResponse; import com.antdsp.common.pagination.PaginationData; import com.antdsp.dao.jpa.UserJpa; +import com.antdsp.dao.jpa.system.RoleJpa; import com.antdsp.data.entity.User; +import com.antdsp.data.entity.system.SystemRole; import com.antdsp.data.entityeenum.UserStatus; +import com.antdsp.web.dto.SystemUserDto; @RestController @RequestMapping("/system/user") @@ -43,6 +47,9 @@ public class SystemUserApi { @Autowired private UserJpa userJpa; + @Autowired + private RoleJpa roleJpa; + @GetMapping("") public PaginationData list(int page , int count , String loginname , UserStatus status){ @@ -69,9 +76,46 @@ public class SystemUserApi { return pagination; } + @GetMapping("/{id:\\d+}") + public SystemUserDto detail(@PathVariable("id") Long userId) { + + List roles = this.roleJpa.findRoleNameAndIds(); + SystemUserDto result = new SystemUserDto(); + result.setRoles(roles); + + if(userId == 0) { + return result; + } + + User user = this.userJpa.findById(userId).get(); + if(user == null) { + return result; + } + String sql = "SELECT s.role_id FROM `tb_role_user` s WHERE s.user_id = :userId "; + Map params = new HashMap<>(); + params.put("userId", userId); + + List list = this.userJpa.queryBySQL(sql, params , null); + List roleIds = new ArrayList<>(); + for(BigInteger item : list) { + roleIds.add(item.longValue()); + } + + result.setRoleIds(roleIds); + user.setPassword(""); + result.setUser(user); + result.setRoles(roles); + + return result; + } + @PostMapping("") @Transactional - public AntdspResponse add(@RequestBody User user) { + public AntdspResponse add(@RequestBody SystemUserDto dto) { + + User user = dto.getUser(); + User current = current(); + List roleIds = dto.getRoleIds(); User olduser = userJpa.queryUserByLoginName(user.getLoginname()); if(olduser != null) { @@ -79,26 +123,35 @@ public class SystemUserApi { } user.setAvatar(""); - user.setCreator(""); - user.setModifier(""); + user.setCreator(current.getLoginname()); + user.setModifier(current.getLoginname()); user.setStatus(UserStatus.NORMAL); user.onPreInsert(); - this.userJpa.save(user); + + Long userId = this.userJpa.save(user).getId(); + this.saveRoleUser(userId, roleIds); return AntdspResponse.success("保存成功"); } @PutMapping("/{id:\\d+}") @Transactional - public AntdspResponse update(@RequestBody User user) { + public AntdspResponse update(@RequestBody SystemUserDto dto) { + + User user = dto.getUser(); + User current = current(); + List roleIds = dto.getRoleIds(); User oldUser = userJpa.getOne(user.getId()); oldUser.setEmail(user.getEmail()); oldUser.setQq(user.getQq()); oldUser.setRealname(user.getRealname()); + oldUser.setModifier(current.getLoginname()); oldUser.onPreUpdate(); this.userJpa.save(oldUser); + this.saveRoleUser(oldUser.getId(), roleIds); + return AntdspResponse.success("保存成功"); } @@ -118,4 +171,18 @@ public class SystemUserApi { current.setPassword(""); return current; } + + private int saveRoleUser(Long userId, List roleIds) { + if(roleIds != null && roleIds.size() > 0) { + StringBuffer sqlStr = new StringBuffer("INSERT INTO `tb_role_user` (`role_id`, `user_id`) VALUES "); + sqlStr.append("("+roleIds.get(0)+ ", " + userId+")"); + if(roleIds.size()>2) { + for(int i= 1 ; i< roleIds.size(); i++) { + sqlStr.append(",("+roleIds.get(i)+ ", " + userId+")"); + } + } + return this.userJpa.executeSQL(sqlStr.toString(), null); + } + return 0; + } } diff --git a/antdsp-admin/src/main/web/src/pages/System/User/Index.js b/antdsp-admin/src/main/web/src/pages/System/User/Index.js index 5eba6ef..5bfafcf 100644 --- a/antdsp-admin/src/main/web/src/pages/System/User/Index.js +++ b/antdsp-admin/src/main/web/src/pages/System/User/Index.js @@ -127,16 +127,16 @@ export default class extends PureComponent { const { dispatch } = this.props; dispatch({ - type: 'systemuser/fetchRoles', - callback:(result)=>{ - this.setState({ - current: currentObj, - confirmLoading: false, - visible: true, - roles: result - }); + type: 'systemuser/fetchOne', + payload:{ + id: currentObj.id || 0 } }); + + this.setState({ + confirmLoading: false, + visible: true + }) }; handlerModalOnOk = e => { @@ -149,20 +149,23 @@ export default class extends PureComponent { form.validateFields((err, fieldsValue) => { if (err) return this.setState({ confirmLoading: false }); - const { current } = this.state; + const { detail } = this.props.systemuser; let password = fieldsValue.password; + const roleIds = fieldsValue.roleIds; + delete fieldsValue.roleIds; if(password){ fieldsValue.password = md5(password); } let currentObj = { - ...current, + ...detail.user, ...fieldsValue, }; dispatch({ type: 'systemuser/save', payload: { user: currentObj, + roleIds: roleIds, }, callback: result => { if (result.success) { @@ -210,10 +213,10 @@ export default class extends PureComponent { render() { const { - systemuser: { UserList }, + systemuser: { UserList, detail }, loading, } = this.props; - + const columns = [ { title: '头像', @@ -308,8 +311,7 @@ export default class extends PureComponent { > (this.formRef = formRef)} - current={this.state.current} - roles={this.state.roles} + detail={detail} /> @@ -345,9 +347,9 @@ export class EditUser extends PureComponent { render() { const { getFieldDecorator } = this.props.form; - const { current, roles } = this.props; + const { detail: { user , roleIds , roles} } = this.props; - const flag = Object.keys(current).length == 0; + const flag = Object.keys(user).length == 0; const formItemLayout = { labelCol: { @@ -365,7 +367,7 @@ export class EditUser extends PureComponent {
{getFieldDecorator('loginname', { - initialValue: current.loginname, + initialValue: user.loginname, rules: [ { required: true, @@ -403,24 +405,25 @@ export class EditUser extends PureComponent { ) : null} {getFieldDecorator('realname', { - initialValue: current.realname, + initialValue: user.realname, })()} {getFieldDecorator('email', { - initialValue: current.email, + initialValue: user.email, })()} {getFieldDecorator('qq', { - initialValue: current.qq, + initialValue: user.qq, })()} { - getFieldDecorator('roles',{ + getFieldDecorator('roleIds',{ + initialValue: flag ? [] : roleIds })( - + ) } diff --git a/antdsp-admin/src/main/web/src/pages/System/User/models/user.js b/antdsp-admin/src/main/web/src/pages/System/User/models/user.js index e7c3a27..ce22860 100644 --- a/antdsp-admin/src/main/web/src/pages/System/User/models/user.js +++ b/antdsp-admin/src/main/web/src/pages/System/User/models/user.js @@ -3,7 +3,8 @@ import { addUser, updateUser, delUserById, - queryRoles + queryRoles, + fetchUser } from '@/services/system'; export default { @@ -13,6 +14,11 @@ export default { UserList:{ data:[], pagination:{} + }, + detail:{ + user: {}, + roleIds:[], + roles: [], } }, @@ -28,9 +34,9 @@ export default { const user = payload.user; let response ; if(!user.id){ - response = yield call(addUser , user); + response = yield call(addUser , payload); }else { - response = yield call(updateUser , user); + response = yield call(updateUser , payload); } if(callback) callback(response); }, @@ -42,6 +48,15 @@ export default { const response = yield call(queryRoles); console.log(response); if(callback) callback(response); + }, + *fetchOne({payload},{call , put}){ + + const response = yield call(fetchUser , payload); + + yield put({ + type: "reducersDetail", + payload: response, + }) } }, @@ -53,6 +68,16 @@ export default { ...action.payload } } + }, + reducersDetail(state, { payload}){ + return { + ...state, + detail: { + user: payload.user || {}, + roleIds: payload.roleIds || [], + roles: payload.roles || [], + }, + } } } } \ No newline at end of file diff --git a/antdsp-admin/src/main/web/src/services/system.js b/antdsp-admin/src/main/web/src/services/system.js index 571166d..b8c42b8 100644 --- a/antdsp-admin/src/main/web/src/services/system.js +++ b/antdsp-admin/src/main/web/src/services/system.js @@ -21,7 +21,8 @@ export async function addUser(params){ } export async function updateUser(params){ - return request(`${USER_API}/${params.id}`,{ + const userId = params.user.id; + return request(`${USER_API}/${userId}`,{ method: 'PUT', data:{ ...params @@ -35,6 +36,12 @@ export async function delUserById(params){ }); } +export async function fetchUser(params){ + return request(`${USER_API}/${params.id}`,{ + method: 'GET' + }); +} + export async function fetchAllMenu(){ return request(`${MENU_API}`) } diff --git a/antdsp-core/src/main/java/com/antdsp/dao/jpa/UserJpa.java b/antdsp-core/src/main/java/com/antdsp/dao/jpa/UserJpa.java index 49432db..52bde26 100644 --- a/antdsp-core/src/main/java/com/antdsp/dao/jpa/UserJpa.java +++ b/antdsp-core/src/main/java/com/antdsp/dao/jpa/UserJpa.java @@ -1,7 +1,5 @@ package com.antdsp.dao.jpa; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -9,7 +7,7 @@ import org.springframework.stereotype.Repository; import com.antdsp.data.entity.User; @Repository("userJpa") -public interface UserJpa extends JpaRepository , JpaSpecificationExecutor{ +public interface UserJpa extends AntdspBaseRepository{ @Query("From User WHERE loginname = :loginName AND status = 'NORMAL'") User queryUserByLoginName(@Param("loginName") String loginName); -- Gitee From f78901c453bd42c8710070886d5f02e2cfdbf56a Mon Sep 17 00:00:00 2001 From: lijiantao Date: Mon, 17 Jun 2019 16:50:41 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../antdsp/common/shiro/AntdspShiroRealm.java | 30 +++++++++++++-- .../main/java/com/antdsp/web/rest/Login.java | 2 +- .../antdsp/web/rest/sys/SystemMenuApi.java | 27 ++++---------- .../antdsp/web/rest/sys/SystemRoleApi.java | 37 ++++++------------- .../antdsp/web/rest/sys/SystemUserApi.java | 29 +++++++-------- .../src/main/resources/application-dev.yml | 3 +- antdsp-admin/src/main/web/src/models/login.js | 7 +++- antdsp-admin/src/main/web/src/models/menu.js | 7 +++- .../src/main/web/src/services/login.js | 4 ++ .../src/main/web/src/services/system.js | 4 ++ .../main/java/com/antdsp/dao/jpa/UserJpa.java | 10 +++++ .../com/antdsp/dao/jpa/system/MenuJpa.java | 13 +++++++ .../com/antdsp/dao/jpa/system/RoleJpa.java | 15 ++++++++ 13 files changed, 120 insertions(+), 68 deletions(-) diff --git a/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java b/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java index 3271f34..c5d4869 100644 --- a/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java +++ b/antdsp-admin/src/main/java/com/antdsp/common/shiro/AntdspShiroRealm.java @@ -1,5 +1,9 @@ package com.antdsp.common.shiro; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; @@ -8,23 +12,43 @@ import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.StringUtils; import com.antdsp.dao.jpa.UserJpa; +import com.antdsp.dao.jpa.system.MenuJpa; import com.antdsp.data.entity.User; +import com.antdsp.data.entity.system.SystemMenu; import com.antdsp.data.entityeenum.UserStatus; public class AntdspShiroRealm extends AuthorizingRealm{ @Autowired private UserJpa userJpa; + + @Autowired + private MenuJpa menuJpa; @Override - protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { - // TODO Auto-generated method stub - return null; + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { + + SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); + + User user = (User) principals.getPrimaryPrincipal(); + List permissMenus = this.menuJpa.findPermissMenus(user.getId()); + Set permisses = new HashSet<>(); + for(SystemMenu menu : permissMenus) { + String permission = menu.getPermission(); + if(!StringUtils.isEmpty(permission)) { + permisses.add(permission); + } + } + + info.addStringPermissions(permisses); + return info; } @Override diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java index cd50c6c..5aeaafb 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/Login.java @@ -31,7 +31,7 @@ public class Login { try { subject.login(token); Session session = subject.getSession(); - session.setTimeout(180000); +// session.setTimeout(180000); return AntdspResponse.success(session.getId().toString()); }catch(AuthenticationException e) { return AntdspResponse.error(e.getMessage()); diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemMenuApi.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemMenuApi.java index 82f2fec..444ca2c 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemMenuApi.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemMenuApi.java @@ -1,19 +1,11 @@ package com.antdsp.web.rest.sys; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; - import org.apache.shiro.SecurityUtils; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.domain.Specification; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -47,9 +39,7 @@ public class SystemMenuApi { rootMenu.setPath("/"); List allMenus = this.menuJpa.findAll(); - this.child(rootMenu, allMenus); - return rootMenu; } @@ -71,7 +61,11 @@ public class SystemMenuApi { @Transactional public AntdspResponse update(@RequestBody MenuTree menu) { - SystemMenu oldMenu = this.menuJpa.getOne(menu.getId()); + SystemMenu oldMenu = this.menuJpa.findById(menu.getId()).orElse(null); + + if(oldMenu == null) { + return AntdspResponse.error("操作失败,未找到菜单信息"); + } this.dtoToData(menu, oldMenu); oldMenu.onPreUpdate(); @@ -82,7 +76,7 @@ public class SystemMenuApi { @DeleteMapping("/{id:\\d+}") public AntdspResponse delete(@PathVariable("id") Long id) { - SystemMenu oldMenu = this.menuJpa.getOne(id); + SystemMenu oldMenu = this.menuJpa.findById(id).orElse(null); if(oldMenu == null) { return AntdspResponse.error("未找到操作对象"); } @@ -101,13 +95,8 @@ public class SystemMenuApi { Subject subject = SecurityUtils.getSubject(); User current = (User) subject.getPrincipal(); - String queryStr = "SELECT sm.* FROM tb_system_menu sm ,tb_role_user ru, tb_role_menu rm "; - queryStr += " WHERE ru.user_id =:userId AND ru.role_id = rm.role_id AND rm.menu_id = sm.id AND sm.menu_type = 'MENU' "; - - Map params = new HashMap<>(); - params.put("userId", 2L); - - List allMenus = this.menuJpa.queryBySQL(queryStr, params, SystemMenu.class); + List allMenuAntButtons = this.menuJpa.findPermissMenus(current.getId()); + List allMenus = allMenuAntButtons.stream().filter(item->MenuType.MENU.equals(item.getMenuType())).collect(Collectors.toList()); MenuTree rootMenu = new MenuTree(); rootMenu.setId(0L); diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemRoleApi.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemRoleApi.java index 2792df7..56a80c8 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemRoleApi.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemRoleApi.java @@ -1,11 +1,7 @@ package com.antdsp.web.rest.sys; -import java.math.BigInteger; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.stream.Stream; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; @@ -35,7 +31,6 @@ import com.antdsp.common.pagination.PaginationData; import com.antdsp.dao.jpa.system.RoleJpa; import com.antdsp.data.entity.system.SystemRole; import com.antdsp.web.dto.RoleDto; -import com.antdsp.web.dto.RoleMenu; /** * @@ -80,23 +75,17 @@ public class SystemRoleApi { @GetMapping("/{id:\\d+}") public RoleDto detail(@PathVariable("id") Long id) { - SystemRole role = this.roleJpa.getOne(id); - RoleDto dto = new RoleDto(); + SystemRole role = this.roleJpa.findById(id).orElse(null); + if(role == null) { + return dto; + } + dto.setDescription(role.getDescription()); dto.setId(role.getId()); dto.setRoleName(role.getRoleName()); - String sqlStr = "SELECT r.menu_id FROM tb_role_menu r LEFT JOIN tb_system_menu sr ON r.menu_id = sr.id WHERE r.role_id = :roleId AND sr.menu_type = 'BUTTON' "; - - Map params = new HashMap<>(); - params.put("roleId", id); - - List list = this.roleJpa.queryBySQL(sqlStr, params , null); - List menuIds = new ArrayList<>(); - for(BigInteger item : list) { - menuIds.add(item.longValue()); - } + List menuIds = this.roleJpa.queryRoleMenuId(id); dto.setMenuIds(menuIds); return dto; } @@ -127,7 +116,7 @@ public class SystemRoleApi { public AntdspResponse update(@RequestBody RoleDto role) { Long roleId = role.getId(); - SystemRole data = this.roleJpa.getOne(roleId); + SystemRole data = this.roleJpa.findById(roleId).orElse(null); if(data == null) { return AntdspResponse.error("未找到角色信息"); } @@ -136,24 +125,22 @@ public class SystemRoleApi { data.onPreUpdate(); this.roleJpa.save(data); - String deleteStr = "DELETE FROM `tb_role_menu` WHERE `role_id` = :roleId"; - Map params = new HashMap<>(); - params.put("roleId", roleId); - - this.roleJpa.executeSQL(deleteStr , params); + this.roleJpa.deleteRoleMenuByRoleId(roleId); this.saveRoleMenu(role.getMenuIds(), roleId); + return AntdspResponse.success(); } @DeleteMapping("/{id:\\d+}") - @Transactional + @Transactional() public AntdspResponse delete(@PathVariable("id") Long id) { - SystemRole role = this.roleJpa.getOne(id); + SystemRole role = this.roleJpa.findById(id).orElse(null); if(role == null) { return AntdspResponse.error("未找到角色信息"); } this.roleJpa.delete(role); + this.roleJpa.deleteRoleMenuByRoleId(role.getId()); return AntdspResponse.success(); } diff --git a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java index 819d1c1..94ecfa7 100644 --- a/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java +++ b/antdsp-admin/src/main/java/com/antdsp/web/rest/sys/SystemUserApi.java @@ -1,10 +1,7 @@ package com.antdsp.web.rest.sys; -import java.math.BigInteger; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; @@ -12,6 +9,7 @@ import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -20,6 +18,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; import org.springframework.data.jpa.domain.Specification; +import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.DeleteMapping; @@ -87,20 +86,12 @@ public class SystemUserApi { return result; } - User user = this.userJpa.findById(userId).get(); + User user = this.userJpa.findById(userId).orElse(null); if(user == null) { return result; } - String sql = "SELECT s.role_id FROM `tb_role_user` s WHERE s.user_id = :userId "; - Map params = new HashMap<>(); - params.put("userId", userId); - - List list = this.userJpa.queryBySQL(sql, params , null); - List roleIds = new ArrayList<>(); - for(BigInteger item : list) { - roleIds.add(item.longValue()); - } + List roleIds = this.userJpa.queryUserRoleId(userId); result.setRoleIds(roleIds); user.setPassword(""); result.setUser(user); @@ -142,7 +133,7 @@ public class SystemUserApi { User current = current(); List roleIds = dto.getRoleIds(); - User oldUser = userJpa.getOne(user.getId()); + User oldUser = userJpa.findById(user.getId()).orElse(null); oldUser.setEmail(user.getEmail()); oldUser.setQq(user.getQq()); @@ -150,17 +141,25 @@ public class SystemUserApi { oldUser.setModifier(current.getLoginname()); oldUser.onPreUpdate(); this.userJpa.save(oldUser); + this.userJpa.deleteUserRoleId(user.getId()); + this.saveRoleUser(oldUser.getId(), roleIds); return AntdspResponse.success("保存成功"); } @DeleteMapping("/{id:\\d+}") + @RequiresPermissions(value= {"user:delete"}) @Transactional public AntdspResponse delete(@PathVariable("id") Long id) { - User oldUser = userJpa.getOne(id); + User oldUser = userJpa.findById(id).orElse(null); + if(oldUser == null) { + return AntdspResponse.error("删除失败,未找到用户信息"); + } this.userJpa.delete(oldUser); + this.userJpa.deleteUserRoleId(id); + return AntdspResponse.success(); } diff --git a/antdsp-admin/src/main/resources/application-dev.yml b/antdsp-admin/src/main/resources/application-dev.yml index 5783a6b..673bd66 100644 --- a/antdsp-admin/src/main/resources/application-dev.yml +++ b/antdsp-admin/src/main/resources/application-dev.yml @@ -9,4 +9,5 @@ spring: jpa: hibernate: ddl-auto: update - show-sql: true \ No newline at end of file + show-sql: true + database-platform: org.hibernate.dialect.MySQL5InnoDBDialect \ No newline at end of file diff --git a/antdsp-admin/src/main/web/src/models/login.js b/antdsp-admin/src/main/web/src/models/login.js index 396b317..122a452 100644 --- a/antdsp-admin/src/main/web/src/models/login.js +++ b/antdsp-admin/src/main/web/src/models/login.js @@ -4,7 +4,7 @@ import { fakeAccountLogin, getFakeCaptcha } from '@/services/api'; import { setAuthority } from '@/utils/authority'; import { getPageQuery } from '@/utils/utils'; import { reloadAuthorized } from '@/utils/Authorized'; -import { loginAction } from '@/services/login' +import { loginAction, logoutAction } from '@/services/login' export default { namespace: 'login', @@ -51,7 +51,10 @@ export default { yield call(getFakeCaptcha, payload); }, - *logout(_, { put }) { + *logout(_, { call , put }) { + + yield call(logoutAction); + yield put({ type: 'changeLoginStatus', payload: { authority: 'guest', sessionId: null}, diff --git a/antdsp-admin/src/main/web/src/models/menu.js b/antdsp-admin/src/main/web/src/models/menu.js index 3d98f2d..9459ff1 100644 --- a/antdsp-admin/src/main/web/src/models/menu.js +++ b/antdsp-admin/src/main/web/src/models/menu.js @@ -3,6 +3,7 @@ import isEqual from 'lodash/isEqual'; import { formatMessage } from 'umi-plugin-react/locale'; import Authorized from '@/utils/Authorized'; import { menu } from '../defaultSettings'; +import { queryRoleMenus } from '@/services/system' const { check } = Authorized; @@ -108,10 +109,12 @@ export default { }, effects: { - *getMenuData({ payload }, { put }) { + *getMenuData({ payload }, {call , put }) { const { routes, authority, path } = payload; const originalMenuData = memoizeOneFormatter(routes, authority, path); - const menuData = filterMenuData(originalMenuData); + // const menuData = filterMenuData(originalMenuData); + const roleMenus = yield call(queryRoleMenus); + const menuData = roleMenus.children || []; const breadcrumbNameMap = memoizeOneGetBreadcrumbNameMap(originalMenuData); yield put({ type: 'save', diff --git a/antdsp-admin/src/main/web/src/services/login.js b/antdsp-admin/src/main/web/src/services/login.js index 86ccf9b..dab0ddb 100644 --- a/antdsp-admin/src/main/web/src/services/login.js +++ b/antdsp-admin/src/main/web/src/services/login.js @@ -10,4 +10,8 @@ export async function loginAction(param){ ...param } }) +} + +export async function logoutAction(param){ + return request(`${API_BASE}/logout`); } \ No newline at end of file diff --git a/antdsp-admin/src/main/web/src/services/system.js b/antdsp-admin/src/main/web/src/services/system.js index b8c42b8..7d58feb 100644 --- a/antdsp-admin/src/main/web/src/services/system.js +++ b/antdsp-admin/src/main/web/src/services/system.js @@ -104,4 +104,8 @@ export async function fetchRoleById(param){ export async function queryRoles(){ return request(`${ROLE_API}/queryRoleNameAndIds`); +} + +export async function queryRoleMenus(){ + return request(`${MENU_API}/route`); } \ No newline at end of file diff --git a/antdsp-core/src/main/java/com/antdsp/dao/jpa/UserJpa.java b/antdsp-core/src/main/java/com/antdsp/dao/jpa/UserJpa.java index 52bde26..0739140 100644 --- a/antdsp-core/src/main/java/com/antdsp/dao/jpa/UserJpa.java +++ b/antdsp-core/src/main/java/com/antdsp/dao/jpa/UserJpa.java @@ -1,5 +1,8 @@ package com.antdsp.dao.jpa; +import java.util.List; + +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -11,5 +14,12 @@ public interface UserJpa extends AntdspBaseRepository{ @Query("From User WHERE loginname = :loginName AND status = 'NORMAL'") User queryUserByLoginName(@Param("loginName") String loginName); + + @Query(value="SELECT s.role_id FROM `tb_role_user` s WHERE s.user_id = :userId ", nativeQuery=true) + List queryUserRoleId(@Param("userId") Long userId); + + @Modifying + @Query(value="DELETE FROM `tb_role_user` WHERE user_id = :userId " , nativeQuery=true) + void deleteUserRoleId(@Param("userId") Long userId); } diff --git a/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/MenuJpa.java b/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/MenuJpa.java index b83e48a..74f9b09 100644 --- a/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/MenuJpa.java +++ b/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/MenuJpa.java @@ -1,5 +1,9 @@ package com.antdsp.dao.jpa.system; +import java.util.List; + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.antdsp.dao.jpa.AntdspBaseRepository; @@ -7,5 +11,14 @@ import com.antdsp.data.entity.system.SystemMenu; @Repository("menuJpa") public interface MenuJpa extends AntdspBaseRepository{ + + /** + * 根据userId查询所有权限菜单 + * @param userId + * @return + */ + @Query(value="SELECT m.* FROM `tb_system_menu` m , `tb_role_menu` rm , `tb_role_user` ru WHERE " + + " rm.role_id = ru.role_id AND rm.menu_id = m.id AND ru.user_id = :userId " , nativeQuery=true) + List findPermissMenus(@Param("userId") Long userId); } diff --git a/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/RoleJpa.java b/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/RoleJpa.java index 32c159f..1457623 100644 --- a/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/RoleJpa.java +++ b/antdsp-core/src/main/java/com/antdsp/dao/jpa/system/RoleJpa.java @@ -2,7 +2,9 @@ package com.antdsp.dao.jpa.system; import java.util.List; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.antdsp.dao.jpa.AntdspBaseRepository; @@ -13,5 +15,18 @@ public interface RoleJpa extends AntdspBaseRepository{ @Query("FROM SystemRole s ORDER BY s.created") List findRoleNameAndIds(); + + /** + * 根据roleId查询角色下所有的菜单信息 + * @param roleId + * @return + */ + @Query(value="SELECT r.menu_id FROM tb_role_menu r LEFT JOIN tb_system_menu sr ON r.menu_id = sr.id WHERE " + + " r.role_id = :roleId AND sr.menu_type = 'BUTTON' " , nativeQuery=true) + List queryRoleMenuId(@Param("roleId")Long roleId); + + @Modifying + @Query(value="DELETE FROM `tb_role_menu` WHERE `role_id` = :roleId", nativeQuery=true) + void deleteRoleMenuByRoleId(@Param("roleId")Long roleId); } -- Gitee