Commit abfbeae0 authored by renjie's avatar renjie

browser-backend

parent d74cb6d2
......@@ -5,7 +5,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
@SpringBootApplication
@EnableAuthorizationServer
public class BrowserBackendApplication {
public static void main(String[] args) {
......
......@@ -74,9 +74,9 @@ public class AccountController {
}
@RequestMapping(path = "/{name}", method = RequestMethod.GET)
public Account getAccountByName(@PathVariable String name) {
return accountService.findByName(name);
@RequestMapping(path = "/information", method = RequestMethod.GET)
public Account getAccount(Principal principal) {
return accountService.findByName(principal.getName());
}
@RequestMapping(path = "/{name}/children/{level}", method = RequestMethod.GET)
......@@ -213,7 +213,6 @@ public class AccountController {
}
@RequestMapping(path = "/wxpay/checkstatus/{tradno}", method = RequestMethod.GET)
@PreAuthorize("#oauth2.hasScope('server')")
public UserPaymentDto wechatPayCheckStatus(@PathVariable String tradno) {
return paymentService.wxCheckOrderStatus(tradno);
}
......@@ -288,20 +287,17 @@ public class AccountController {
return accountService.invoiceSize(principal.getName());
}
@PreAuthorize("#oauth2.hasScope('server')")
@RequestMapping(path = "/cellphone/{cellphone}", method = RequestMethod.GET)
public AccountDto getAccountByCellphone(@PathVariable String cellphone) {
return accountService.getAccountByCellphone(cellphone);
}
@PreAuthorize("#oauth2.hasScope('server')")
@RequestMapping(path = "/fee/{region}/{instanceSpecKey}/{chargeType}", method = RequestMethod.GET)
@Deprecated
public String queryVpsRate(@PathVariable String region, @PathVariable String instanceSpecKey, @PathVariable String chargeType) {
return accountService.queryVpsRate(region, instanceSpecKey, chargeType);
}
@PreAuthorize("#oauth2.hasScope('server')")
@RequestMapping(path = "/charge/rate/{username}", method = RequestMethod.POST)
public String queryUserChargeRate(@PathVariable String username, @RequestBody IpChargeRequestDto requestDto) {
return accountService.queryUserChargeRate(username, requestDto);
......@@ -361,13 +357,11 @@ public class AccountController {
}
@RequestMapping(path = "/preorder/{userName}", method = RequestMethod.POST)
@PreAuthorize("#oauth2.hasScope('server')")
public IpChargeResultDto preOrderOption(@PathVariable String userName, @RequestBody IpChargeRequestDto requestDto) {
return accountService.preOrder(userName, requestDto);
}
@RequestMapping(path = "/preorder/delete/{username}", method = RequestMethod.POST)
@PreAuthorize("#oauth2.hasScope('server')")
public String deletePreOrder( @PathVariable("username") String username) {
return accountService.deletePreOrder(username);
}
......
......@@ -38,21 +38,7 @@ public class Account {
private boolean allowedToCreateSubUser = false;
private boolean allowedToCreateCloudAccount = false;
private List<UserService> userServices = new ArrayList<>();
public List<UserService> getUserServices() {
return userServices;
}
public void addUserService(UserService service) {
this.userServices.add(service);
}
public void setUserServices(List<UserService> userServices) {
this.userServices = userServices;
}
private boolean hasOperationRight = false;
public Date getSignupDate() {
return signupDate;
......@@ -70,25 +56,17 @@ public class Account {
isPrePaid = prePaid;
}
public boolean isAllowedToCreateCloudAccount() {
return allowedToCreateCloudAccount;
}
public void setAllowedToCreateCloudAccount(boolean allowedToCreateCloudAccount) {
this.allowedToCreateCloudAccount = allowedToCreateCloudAccount;
}
public Account() {
}
public Account(AccountDto account) {
this.setName(account.getName());
this.setAllowedToCreateSubUser(account.isAllowedToCreateSubUser());
this.setAllowedToCreateCloudAccount(account.isAllowedToCreateCloudAccount());
this.setCompanyName(account.getCompanyName());
this.setEmail(account.getEmail());
this.setJobTitle(account.getJobTitle());
this.setPhoneNumber(account.getPhoneNumber());
this.setHasOperationRight(account.isHasOperationRight());
}
public String getEmail() {
......@@ -182,5 +160,11 @@ public class Account {
this.token = token;
}
public boolean isHasOperationRight() {
return hasOperationRight;
}
public void setHasOperationRight(boolean hasOperationRight) {
this.hasOperationRight = hasOperationRight;
}
}
......@@ -26,7 +26,7 @@ public class AccountDto {
private boolean allowedToCreateSubUser;
private boolean allowedToCreateCloudAccount;
private boolean hasOperationRight = false;
private int balance;
......@@ -43,12 +43,10 @@ public class AccountDto {
public AccountDto(Account account) {
this.setName(account.getName());
this.setAllowedToCreateSubUser(account.isAllowedToCreateSubUser());
this.setAllowedToCreateCloudAccount(account.isAllowedToCreateCloudAccount());
this.setCompanyName(account.getCompanyName());
this.setEmail(account.getEmail());
this.setJobTitle(account.getJobTitle());
this.setPhoneNumber(account.getPhoneNumber());
this.setUserServices(account.getUserServices());
this.setParent(account.getParent());
this.setToken(account.getToken());
}
......@@ -69,22 +67,12 @@ public class AccountDto {
this.balance = balance;
}
private List<UserService> userServices = new ArrayList<>();
public List<UserService> getUserServices() {
return userServices;
}
public void setUserServices(List<UserService> userServices) {
this.userServices = userServices;
}
public boolean isAllowedToCreateCloudAccount() {
return allowedToCreateCloudAccount;
public boolean isHasOperationRight() {
return hasOperationRight;
}
public void setAllowedToCreateCloudAccount(boolean allowedToCreateCloudAccount) {
this.allowedToCreateCloudAccount = allowedToCreateCloudAccount;
public void setHasOperationRight(boolean hasOperationRight) {
this.hasOperationRight = hasOperationRight;
}
public String getName() {
......
......@@ -42,7 +42,7 @@ public class User {
private boolean allowedToCreateSubUser = true;
private boolean allowedToCreateCloudAccount = true;
private boolean hasOperationRight = false;
private String verificationCode= UUID.randomUUID().toString();
......@@ -96,13 +96,14 @@ public class User {
allowedToCreateSubUser = allowedToCreateSubUser;
}
public boolean isAllowedToCreateCloudAccount() {
return allowedToCreateCloudAccount;
public boolean isHasOperationRight() {
return hasOperationRight;
}
public void setAllowedToCreateCloudAccount(boolean allowedToCreateCloudAccount) {
this.allowedToCreateCloudAccount = allowedToCreateCloudAccount;
public void setHasOperationRight(boolean hasOperationRight) {
this.hasOperationRight = hasOperationRight;
}
public String getLockReason() {
return lockReason;
}
......
package com.edgec.browserbackend.account.domain;
import java.util.Date;
public class UserService {
private Services serviceName;
private ServiceType serviceType;
private Date validTo;
// 0 - 100 percents
private int discount = 20;
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public Services getServiceName() {
return serviceName;
}
public void setServiceName(Services serviceName) {
this.serviceName = serviceName;
}
public ServiceType getServiceType() {
return serviceType;
}
public void setServiceType(ServiceType serviceType) {
this.serviceType = serviceType;
}
public Date getValidTo() {
return validTo;
}
public void setValidTo(Date validTo) {
this.validTo = validTo;
}
}
......@@ -493,7 +493,6 @@ public class AccountServiceImpl implements AccountService {
return null;
}
if (StringUtils.isEmpty(account.getParent())) {
account.setAllowedToCreateCloudAccount(true);
account.setAllowedToCreateSubUser(true);
}
return account;
......@@ -565,9 +564,6 @@ public class AccountServiceImpl implements AccountService {
account.setAllowedToCreateSubUser(true);
account.setAllowedToCreateCloudAccount(true);
user.setEnabled(true);
userAuthService.create(new com.edgec.browserbackend.auth.domain.User(user));
......@@ -604,20 +600,10 @@ public class AccountServiceImpl implements AccountService {
if (StringUtils.isEmpty(parentName)) {
account.setAllowedToCreateSubUser(true);
account.setAllowedToCreateCloudAccount(true);
UserService intelligroupService = new UserService();
intelligroupService.setServiceName(Services.INTELLIGROUP);
intelligroupService.setServiceName(Services.INTELLIGROUP);
intelligroupService.setServiceType(ServiceType.TRIAL);
// intelligroupService.setUsername(user.getUsername());
intelligroupService.setValidTo(Date.from(ZonedDateTime.now().plusMonths(TRIVAL_MONTHS).toInstant()));
account.addUserService(intelligroupService);
account.setHasOperationRight(true);
} else {
account.setAllowedToCreateSubUser(user.isAllowedToCreateSubUser());
account.setAllowedToCreateCloudAccount(user.isAllowedToCreateCloudAccount());
account.setHasOperationRight(user.isHasOperationRight());
}
account.setParent(parentName);
......@@ -721,6 +707,7 @@ public class AccountServiceImpl implements AccountService {
authUser.setPhone(user.getPhoneNumber());
authUser.setEmail(user.getEmail());
authUser.setEnabled(true);
authUser.setHasOperationRight(user.isHasOperationRight());
userAuthService.create(new com.edgec.browserbackend.auth.domain.User(authUser));
Account account = new Account(user);
......@@ -741,10 +728,10 @@ public class AccountServiceImpl implements AccountService {
throw new ClientRequestException(AccountErrorCode.UNKNOWN, "Invalid Request");
}
childAccount.setAllowedToCreateSubUser(user.isAllowedToCreateSubUser());
childAccount.setAllowedToCreateCloudAccount(user.isAllowedToCreateCloudAccount());
childAccount.setPhoneNumber(user.getPhoneNumber());
childAccount.setJobTitle(user.getJobTitle());
childAccount.setCompanyName(user.getCompanyName());
childAccount.setHasOperationRight(user.isHasOperationRight());
repository.save(childAccount);
return childAccount;
}
......@@ -765,6 +752,7 @@ public class AccountServiceImpl implements AccountService {
account.setJobTitle(update.getJobTitle());
// account.setPhoneNumber(update.getPhoneNumber());
account.setEmail(update.getEmail());
account.setHasOperationRight(update.isHasOperationRight());
log.debug("account {} changes has been saved", name);
if (!org.apache.commons.lang3.StringUtils.equalsIgnoreCase(preEmail, update.getEmail())) {
Account account1 = repository.findByEmail(update.getEmail());
......
......@@ -10,6 +10,7 @@ import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
......@@ -19,6 +20,7 @@ import org.springframework.security.oauth2.provider.token.store.InMemoryTokenSto
* @author cdov
*/
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
private final String NOOP_PASSWORD_ENCODE = "{noop}";
......
package com.edgec.browserbackend.auth.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
private Logger logger = LoggerFactory.getLogger(ResourceServerConfiguration.class);
@Override
public void configure(HttpSecurity http) throws Exception {
logger.info("=========================111111111=========");
http.exceptionHandling()
.and()
.logout()
.logoutUrl("/oauth/logout")
.and()
.authorizeRequests()
.antMatchers("/browser/").permitAll()
.antMatchers("/secure/**").authenticated();
}
}
\ No newline at end of file
......@@ -30,7 +30,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/users/verify/**", "/users/changepass", "/account/authCode", "/account/signup");
.antMatchers("/users/verify/**", "/users/changepass", "/account/authCode", "/account/signup",
"/account/reset*", "/token");
}
@Override
......
package com.edgec.browserbackend.auth.controller;
import com.edgec.browserbackend.auth.domain.UsernameToken;
import com.edgec.browserbackend.auth.service.UsernameTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
import java.util.List;
@RestController
public class TokenController {
@Autowired
private UsernameTokenService usernameTokenService;
@RequestMapping(path = "/token", method = RequestMethod.GET)
public List<UsernameToken> selectVpsDtoByParent(Principal principal) {
return usernameTokenService.selectVpsDtoByParent(principal.getName());
}
@RequestMapping(path = "/token", method = RequestMethod.POST)
public UsernameToken createToken(Principal principal, @RequestParam(value = "username") String username) {
return usernameTokenService.createUserToken(principal.getName(), username);
}
}
......@@ -111,4 +111,8 @@ public class User implements UserDetails {
setVerificationCode(user.getVerificationCode());
setEnabled(user.isEnabled());
}
public User(){
};
}
package com.edgec.browserbackend.auth.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "usernametoken")
@JsonIgnoreProperties(ignoreUnknown = true)
public class UsernameToken {
@Id
private String username;
private String token;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
package com.edgec.browserbackend.auth.repository;
import com.edgec.browserbackend.auth.domain.UsernameToken;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UsernameTokenRepository extends MongoRepository<UsernameToken,String> {
UsernameToken findByUsername(String username);
}
package com.edgec.browserbackend.auth.service;
package com.edgec.browserbackend.auth.service.Impl;
import com.edgec.browserbackend.auth.domain.Roles;
import com.edgec.browserbackend.auth.domain.User;
......@@ -6,6 +6,7 @@ import com.edgec.browserbackend.auth.domain.UserPasswordReset;
import com.edgec.browserbackend.auth.exception.AuthErrorCode;
import com.edgec.browserbackend.auth.repository.RolesRepository;
import com.edgec.browserbackend.auth.repository.UserRepository;
import com.edgec.browserbackend.auth.service.UserAuthService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
......
package com.edgec.browserbackend.auth.service.Impl;
import com.edgec.browserbackend.account.domain.Account;
import com.edgec.browserbackend.account.domain.AccountDto;
import com.edgec.browserbackend.account.domain.UserDto;
import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.auth.domain.UsernameToken;
import com.edgec.browserbackend.auth.exception.AuthErrorCode;
import com.edgec.browserbackend.auth.repository.UsernameTokenRepository;
import com.edgec.browserbackend.auth.service.UsernameTokenService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
@Service
public class UsernameTokenServiceImpl implements UsernameTokenService {
@Autowired
private UsernameTokenRepository usernameTokenRepository;
@Autowired
private AccountService accountSecrvice;
@Override
public List<UsernameToken> selectVpsDtoByParent(String username) {
List<UserDto> userDtos = accountSecrvice.getAllDesendentUsers(username, 0);
List<UsernameToken> usernameTokens = userDtos.stream().map(userDto -> {
return usernameTokenRepository.findByUsername(userDto.getUsername());
}).filter(Objects::nonNull).collect(Collectors.toList());
UsernameToken parentToken = usernameTokenRepository.findByUsername(username);
if (parentToken != null) {
usernameTokens.add(parentToken);
}
return usernameTokens;
}
public UsernameToken createUsername(String username) {
String token = UUID.randomUUID().toString();
UsernameToken usernameToken = new UsernameToken();
usernameToken.setToken(token);
usernameToken.setUsername(username);
usernameTokenRepository.save(usernameToken);
return usernameToken;
}
@Override
public UsernameToken getUserToken(String username) {
return usernameTokenRepository.findByUsername(username);
}
public UsernameToken createUserToken(String owner, String targetUser) {
Account account = accountSecrvice.findByName(targetUser);
if (owner.equals(account.getParent()) || owner.equals(targetUser)) {
String token = UUID.randomUUID().toString().replace("-","");
UsernameToken usernameToken = new UsernameToken();
usernameToken.setToken(token);
usernameToken.setUsername(targetUser);
usernameTokenRepository.save(usernameToken);
return usernameToken;
}
throw new ClientRequestException(AuthErrorCode.AUTHORIZATION_ERROR);
}
}
package com.edgec.browserbackend.auth.service;
import com.edgec.browserbackend.auth.domain.UsernameToken;
import java.util.List;
public interface UsernameTokenService {
List<UsernameToken> selectVpsDtoByParent(String username);
UsernameToken createUserToken(String owner, String targetUser);
UsernameToken getUserToken(String username);
}
package com.edgec.browserbackend.browser.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@Document(collection = "ipresource")
public class IpResource {
@Id
private String id;
......
package com.edgec.browserbackend.browser.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@Document("usershops")
public class UserShops {
@Id
private String username;
private List<String> shops;
private List<String> shopIds;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment