소스 검색

一体化登录

hym 4 년 전
부모
커밋
69b7bf4586

+ 8 - 1
smart-city-platform/src/main/java/com/bz/smart_city/commom/security/WebSecurityConfig.java

@@ -7,6 +7,7 @@ import com.bz.smart_city.commom.security.assistant.openid.OpenidSecurityConfig;
 import com.bz.smart_city.commom.security.integration.IntegrationSecurityConfig;
 import com.bz.smart_city.commom.security.mobile.MobileLoginUserDetailService;
 import com.bz.smart_city.commom.security.mobile.MobileSecurityConfig;
+import com.bz.smart_city.commom.security.platform.PlatformSecurityConfig;
 import com.bz.smart_city.commom.security.smsCode.SmsCodeCheckUserFilter;
 import com.bz.smart_city.commom.security.smsCode.SmsCodeSecurityConfig;
 import com.bz.smart_city.commom.security.validate.ValidateCodeCheckFilter;
@@ -80,6 +81,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
     private IntegrationSecurityConfig integrationSecurityConfig;
     @Autowired
     private AppSecurityConfig appSecurityConfig;
+    @Autowired
+    private PlatformSecurityConfig platformSecurityConfig;
 
     @Bean
     public PasswordEncoder passwordEncoder() {
@@ -112,6 +115,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/platform/**")
                 .antMatchers("/device/valve/update").antMatchers("/message/generatedMessage")
                 .antMatchers("/integration/user/save","/integration/user/del")
+                .antMatchers("/user/bindUniqId")
                 .antMatchers("/rabbit/**").antMatchers("/meter/updateUserCode","/meter/getMeterReadList");
 
     }
@@ -154,7 +158,10 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .and()
                 .apply(integrationSecurityConfig)
                 .and()
-                .apply(appSecurityConfig);
+                .apply(appSecurityConfig)
+                .and().
+                apply(platformSecurityConfig)
+        ;
 
     }
 

+ 5 - 5
smart-city-platform/src/main/java/com/bz/smart_city/commom/security/platform/PlatformAuthenticationProvider.java

@@ -20,23 +20,23 @@ public class PlatformAuthenticationProvider implements AuthenticationProvider {
 
     @Override
     public Authentication authenticate(Authentication authentication) throws AuthenticationException {
-        MobileLoginAuthenticationToken authenticationToken = (MobileLoginAuthenticationToken)authentication;
+        PlatformAuthenticationToken authenticationToken = (PlatformAuthenticationToken) authentication;
         UserDetails userDetails = userDetailsService.loadUserByUsername((String) authenticationToken.getPrincipal());
         if (userDetails == null) {
             throw new InternalAuthenticationServiceException("无法获取用户信息");
         }
         String presentedPassword = authenticationToken.getCredentials().toString();
 
-        MobileLoginAuthenticationToken authenticationTokenReslut = new MobileLoginAuthenticationToken(userDetails,authenticationToken.getCredentials(),userDetails.getAuthorities());
+        PlatformAuthenticationToken platformAuthenticationToken = new PlatformAuthenticationToken(userDetails,authenticationToken.getCredentials(),userDetails.getAuthorities());
 
-        authenticationTokenReslut.setDetails(authenticationToken.getDetails());
+        platformAuthenticationToken.setDetails(authenticationToken.getDetails());
 
-        return authenticationTokenReslut;
+        return platformAuthenticationToken;
     }
 
     @Override
     public boolean supports(Class<?> authentication) {
-        return MobileLoginAuthenticationToken.class.isAssignableFrom(authentication);
+        return PlatformAuthenticationToken.class.isAssignableFrom(authentication);
     }
 
     public void setPasswordEncoder(PasswordEncoder passwordEncoder) {

+ 58 - 0
smart-city-platform/src/main/java/com/bz/smart_city/commom/security/platform/PlatformAuthenticationToken.java

@@ -0,0 +1,58 @@
+package com.bz.smart_city.commom.security.platform;
+
+import org.springframework.security.authentication.AbstractAuthenticationToken;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.SpringSecurityCoreVersion;
+
+import java.util.Collection;
+
+public class PlatformAuthenticationToken  extends AbstractAuthenticationToken {
+    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
+
+    /**
+     * 手机号
+     */
+    private final Object principal;
+    private Object credentials;
+
+
+    public PlatformAuthenticationToken(String mobile, Object credentials) {
+        super(null);
+        this.principal = mobile;
+        this.credentials = credentials;
+        setAuthenticated(false);
+    }
+
+    public PlatformAuthenticationToken(Object principal,Object credentials, Collection<? extends GrantedAuthority> authorities) {
+        super(authorities);
+        this.principal = principal;
+        this.credentials = credentials;
+        super.setAuthenticated(true);
+    }
+
+    @Override
+    public Object getCredentials() {
+        return this.credentials;
+    }
+
+    @Override
+    public Object getPrincipal() {
+        return this.principal;
+    }
+    /**
+     * @param isAuthenticated
+     * @throws IllegalArgumentException
+     */
+    @Override
+    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
+        if (isAuthenticated) {
+            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
+        }
+        super.setAuthenticated(false);
+    }
+
+    @Override
+    public void eraseCredentials() {
+        super.eraseCredentials();
+    }
+}

+ 1 - 1
smart-city-platform/src/main/java/com/bz/smart_city/commom/security/platform/PlatformCheckUserFilter.java

@@ -37,7 +37,7 @@ public class PlatformCheckUserFilter extends OncePerRequestFilter {
 
     @Override
     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
-        if (StringUtils.equals(contextPath+"/user/platformLogin", request.getRequestURI()) && StringUtils.equalsIgnoreCase(request.getMethod(), "post")) {
+        if (StringUtils.equals(contextPath+"/user/platformLogin888", request.getRequestURI()) && StringUtils.equalsIgnoreCase(request.getMethod(), "post")) {
             try {
                 validate(new ServletWebRequest(request));
             } catch (SmsCodeException e) {

+ 2 - 2
smart-city-platform/src/main/java/com/bz/smart_city/commom/security/platform/PlatformLoginAuthenticationFilter.java

@@ -46,7 +46,7 @@ public class PlatformLoginAuthenticationFilter extends AbstractAuthenticationPro
 
         mobile = mobile.trim();
 
-        MobileLoginAuthenticationToken authRequest = new MobileLoginAuthenticationToken(mobile, "");
+        PlatformAuthenticationToken authRequest = new PlatformAuthenticationToken(mobile, "");
 
         // Allow subclasses to set the "details" property
         setDetails(request, authRequest);
@@ -64,7 +64,7 @@ public class PlatformLoginAuthenticationFilter extends AbstractAuthenticationPro
     }
 
 
-    protected void setDetails(HttpServletRequest request, MobileLoginAuthenticationToken authRequest) {
+    protected void setDetails(HttpServletRequest request, PlatformAuthenticationToken authRequest) {
         authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
     }
 

+ 2 - 0
smart-city-platform/src/main/java/com/bz/smart_city/commom/security/platform/PlatformLoginUserDetailService.java

@@ -82,7 +82,9 @@ public class PlatformLoginUserDetailService implements UserDetailsService {
 
         if (user == null) {
             String key= UUID.randomUUID().toString();
+            if(uniqId!=null)
             redisUtil.set(key,uniqId);
+            redisUtil.setExpire(key,1800);
             throw new PlatformException(key);
         }
 

+ 8 - 9
smart-city-platform/src/main/java/com/bz/smart_city/commom/security/platform/PlatformSecurityConfig.java

@@ -36,15 +36,14 @@ public class PlatformSecurityConfig extends SecurityConfigurerAdapter<DefaultSec
 
     @Override
     public void configure(HttpSecurity http) throws Exception {
-        PlatformLoginAuthenticationFilter smsCodeLoginAuthenticationFilter = new PlatformLoginAuthenticationFilter();
-        smsCodeLoginAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
-        smsCodeLoginAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
-        smsCodeLoginAuthenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler);
+        PlatformLoginAuthenticationFilter platformLoginAuthenticationFilter = new PlatformLoginAuthenticationFilter();
+        platformLoginAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
+        platformLoginAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
+        platformLoginAuthenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler);
 
-        PlatformAuthenticationProvider smsCodeLoginAuthenticationProvider = new PlatformAuthenticationProvider();
-        smsCodeLoginAuthenticationProvider.setUserDetailsService(userDetailsService);
-        http.authenticationProvider(smsCodeLoginAuthenticationProvider)
-                .addFilterAfter(smsCodeCheckUserFilter, UsernamePasswordAuthenticationFilter.class)
-                .addFilterAfter(smsCodeLoginAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
+        PlatformAuthenticationProvider platformAuthenticationProvider = new PlatformAuthenticationProvider();
+        platformAuthenticationProvider.setUserDetailsService(userDetailsService);
+        http.authenticationProvider(platformAuthenticationProvider)
+                .addFilterAfter(platformLoginAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
     }
 }

+ 1 - 1
smart-city-platform/src/main/java/com/bz/smart_city/dao/UserMapper.java

@@ -75,6 +75,6 @@ public interface UserMapper {
 
     User findUserByUniqId(String uniqId);
 
-    int bindUniqId(String phone, String uniqId);
+    int bindUniqId(@Param("phone") String phone,@Param("uniqId")  String uniqId);
 }
 

+ 2 - 15
smart-city-platform/src/main/java/com/bz/smart_city/service/impl/WarningLogServiceImpl.java

@@ -3,6 +3,7 @@ package com.bz.smart_city.service.impl;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.bz.smart_city.commom.model.Pagination;
+import com.bz.smart_city.commom.util.HttpClientUtils;
 import com.bz.smart_city.commom.util.UserUtil;
 import com.bz.smart_city.dao.MeterReadRecordMapper;
 import com.bz.smart_city.dao.WarningRuleMapper;
@@ -66,7 +67,7 @@ public class WarningLogServiceImpl implements WarningLogService{
         });
         Map<String,Object>args=new HashMap<>();
         args.put("metercodes", meterCodes);
-        String result = sendApiReQuest(accountUserInfoUrl, JSONObject.toJSONString(args));
+        String result = "";//HttpClientUtils.sendApiReQuest(accountUserInfoUrl, JSONObject.toJSONString(args));
         if(result!=null){
             JSONArray array = JSONArray.parseArray(result);
             list.forEach(warningLogDto -> {
@@ -83,21 +84,7 @@ public class WarningLogServiceImpl implements WarningLogService{
 
         return new Pagination<>(list);
     }
-    private String sendApiReQuest(String path , String args){
-        HttpHeaders headers = new HttpHeaders();
-        headers.setContentType(MediaType.APPLICATION_JSON);
-        HttpEntity<String> formEntity = new HttpEntity<>(args, headers);
 
-        RestTemplate restTemplate=new RestTemplate();
-        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(path, formEntity,
-                String.class);
-        JSONObject info = JSONObject.parseObject(stringResponseEntity.getBody());
-        String result=null;
-        if(info.getInteger("status")==0){
-            result=info.getString("data");
-        }
-        return result;
-    }
 
     @Override
     public List<BuildingSelectDto> getAreaList(Integer channelId, String deviceNo, Integer warningType, String clientName, Integer feedbackStatus, LocalDateTime startDate, LocalDateTime endDate) {