Spring file upload goes to Access denied page

Spring file upload goes to Access denied page

When Spring security is enabled, when I upload a file using multipart, SpringSecurity redirects me to Access denied url.

Refer to https://stackoverflow.com/questions/28225359/file-upload-spring-security
I amend my Initializer as follows!


@EnableScheduling
@EnableAsync
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer{
@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
insertFilters(servletContext, new MultipartFilter());
}
}

This error disappears. I landed in another problem. I’m resolving it now!!

Advertisements

Spring 5 migration: Unable to find setter method for attribute: [commandName]

Spring 5 Migration brings this pain. You will get this error during form submission.

Unable to find setter method for attribute: [commandName]

Command object does not work anymore.

Refer to https://stackoverflow.com/questions/46989072/org-apache-jasper-jasperexception-web-inf-views-home-jsp-line-25-column/48364974
and
https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-5.x

commandName is dead in Spring 5. Replace it with modelAttribute


<form:form modelAttribute="customer" action="customerView" method="post">

It worked! I need to make this change in my workspace now!

Migrating to Hibernate 5: Table ‘crm.hibernate_sequence’ doesn’t exist

I’m doing upgrading Spring 4 to Spring 5 and Hibernate 4 to 5.
My existing functionality refused to work. It broke with the following exception.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘crm.hibernate_sequence’ doesn’t exist

I’m using MariaDB/MySQL. I don’t have to maintain any sequence tables as Oracle. In addition, it was working without any issues. All my primary keys are autogenerated with the following annotations.

@Id
@GeneratedValue
@Column(name=”ID”)
private Long id;

Thanks to https://stackoverflow.com/questions/32968527/hibernate-sequence-doesnt-exist

Adding hibernate.id.new_generator_mappings=false to Hibernate properties solved the problem.

Eclipse tomcat does not start – May be locked by another process.

Happy Western New Year 2019!

I had a single spring web project. It became too big over the day. I had to split it up into multiple reusable projects so that I can build APIs. After I split it, Tomcat refused to start with the following error.


Publishing failed with multiple errors
Could not delete D:/Pandian/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/crm-upload/WEB-INF/lib. May be locked by another process.
Could not delete D:/Pandian/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/crm-upload/WEB-INF. May be locked by another process.

I tried to clean work folder, It doesn’t work. I did the following sequence to resolve this.

  1. Remove the tomcat server from eclipse server tab
  2. Restart Eclipse
  3. Add a new tomcat server
  4. Start the project

 

Read properties values in Java Spring Controller

To configure the environmental parameters, we read the values from property files. I have a controller which upload the files. The uploaded files will be saved in a location, which is defined in a property file. In this example I shall show you how to read the location name from the property file.


@Controller
@PropertySource("classpath:app.properties")
public class FileUploadController {
  @Value("${upload.Folder}")
  String uploadFolder;
}

I have defined the property in app.properties file which is saved in src folder (or any other path defined in classpath.


upload.Folder=C:\\vss\\upload

Thats it! The variable uploadFolder will be assigned using the values given by app.properties.

Increase the partition size of a Unix VM in XenServer

Distribution: Ubuntu 18.04

This is how I increased the disk partition size of my Ubuntu virtual machine running in XenServer.

Using fdisk, view the partition information, delete the old partition, add a new partition with extended size, save the partition. This is the workflow followed below. Pls note that the partition will not be commited, until you confirm.


# fdisk /dev/xvda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p

Disk /dev/xvda: 429.5 GB, 429496729600 bytes, 838860800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b3935

Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2099199 1048576 83 Linux
/dev/xvda2 2099200 419430399 208665600 8e Linux LVM

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): p

Disk /dev/xvda: 429.5 GB, 429496729600 bytes, 838860800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b3935

Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2099199 1048576 83 Linux

Command (m for help): ^C
# fdisk /dev/xvda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p

Disk /dev/xvda: 429.5 GB, 429496729600 bytes, 838860800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b3935

Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2099199 1048576 83 Linux
/dev/xvda2 2099200 419430399 208665600 8e Linux LVM

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): p

Disk /dev/xvda: 429.5 GB, 429496729600 bytes, 838860800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b3935

Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2099199 1048576 83 Linux
/dev/xvda2 2099200 419430399 208665600 8e Linux LVM

Command (m for help): d
Partition number (1,2, default 2):
Partition 2 is deleted

Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (2099200-838860799, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-838860799, default 838860799):
Using default value 838860799
Partition 2 of type Linux and of size 399 GiB is set

Command (m for help): p

Disk /dev/xvda: 429.5 GB, 429496729600 bytes, 838860800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b3935

Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2099199 1048576 83 Linux
/dev/xvda2 2099200 838860799 418380800 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
# fdisk -l

Disk /dev/xvda: 429.5 GB, 429496729600 bytes, 838860800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b3935

Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2099199 1048576 83 Linux
/dev/xvda2 2099200 838860799 418380800 83 Linux

Disk /dev/mapper/cl_mgr-root: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/cl_mgr-swap: 3892 MB, 3892314112 bytes, 7602176 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/cl_mgr-home: 156.1 GB, 156086829056 bytes, 304857088 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Docker Commands to start/stop containers

Getting docker images

To search for available images in the repository..

$ docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4585                [OK]
ansible/centos7-ansible            Ansible on Centos7                              115                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   99                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   60                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              44                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      43
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   38
gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glust…   32                                      [OK]
openshift/base-centos7             A Centos7 derived base image for Source-To-I…   31
centos/python-35-centos7           Platform for building and running Python 3.5…   28
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   26
kinogmt/centos-ssh                 CentOS with SSH                                 22                                      [OK]
openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use w…   14
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   7
openshift/mysql-55-centos7         DEPRECATED: A Centos7 based MySQL v5.5 image…   6
openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x ima…   4
openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for use …   4
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   2
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   1
jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                                       [OK]
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   0
smartentry/centos                  centos with smartentry                          0                                       [OK]
pivotaldata/centos7-build          CentosOS 7 image for GPDB compilation           0

To download the image from the search results..

$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
256b176beaff: Pull complete
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest

After downloading the image, we may run the instance.

$ docker run centos

Now, we have downloaded many images. To see the list of images..

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              5182e96772bf        10 days ago         200MB
ubuntu              latest              735f80812f90        2 weeks ago         83.5MB
hello-world         latest              2cb0d9787c4d        5 weeks ago         1.85kB

To run an interactive shell of centos, lets run it.

$ docker run -it centos
[root@53771652af4a /]# hostname
53771652af4a
[root@53771652af4a /]# id
uid=0(root) gid=0(root) groups=0(root)

Customizing the containers

Let’s modify this container a bit. For example. following is how we install net-tools package

[root@53771652af4a /]# ifconfig
bash: ifconfig: command not found
[root@53771652af4a /]# yum install net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirror.0x.sg
* extras: mirror.0x.sg
* updates: mirror.0x.sg
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 166 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 174 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 5.9 MB 00:00:00
(4/4): updates/7/x86_64/primary_db | 5.0 MB 00:00:01
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.22.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================================
Installing:
net-tools x86_64 2.0-0.22.20131004git.el7 base 305 k

Transaction Summary
======================================================================================================================================================
Install 1 Package

Total download size: 305 k
Installed size: 917 k
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.22.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.22.20131004git.el7.x86_64.rpm is not installed
net-tools-2.0-0.22.20131004git.el7.x86_64.rpm | 305 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) "
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-5.1804.1.el7.centos.x86_64 (@Updates)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : net-tools-2.0-0.22.20131004git.el7.x86_64 1/1
Verifying : net-tools-2.0-0.22.20131004git.el7.x86_64 1/1

Installed:
net-tools.x86_64 0:2.0-0.22.20131004git.el7

Complete!
[root@53771652af4a /]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8448 bytes 12601933 (12.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4833 bytes 321266 (313.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Similarly I have installed openssh server as well. As we customized our container now, we need to commit it to retain the updated copy. To commit it, lets exit the container shell and execute the following command from the docker machine.

$ docker commit -m "added net-tools and ssh server" -a "pandian" 53771652af4a pandian/centos_ssh
sha256:7f019be4424c070511efb90e357b720333df5d80f845de6a7d569b94ac829fd7

To get the container id, you may use docker ps command as below.

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
53771652af4a        centos              "/bin/bash"         27 minutes ago      Exited (0) 7 minutes ago                        relaxed_vaughan
f6ed55b0451c        centos              "/bin/bash"         33 minutes ago      Exited (0) 33 minutes ago                       relaxed_rosalind
462658ae9e5a        ubuntu              "bash"              15 hours ago        Up 15 hours                                     inspiring_mahavira
1aebd1e64faa        ubuntu              "bash"              15 hours ago        Exited (127) 15 hours ago                       gracious_banach
ff56bc579e04        hello-world         "/hello"            15 hours ago        Exited (0) 15 hours ago                         distracted_lamarr
e209861d337d        hello-world         "/hello"            15 hours ago        Exited (0) 15 hours ago                         laughing_ramanujan

As we commited our image, lets see the image list now. the repository has the updated image.

$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
pandian/centos_ssh   latest              7f019be4424c        25 minutes ago      294MB
centos               latest              5182e96772bf        10 days ago         200MB
ubuntu               latest              735f80812f90        3 weeks ago         83.5MB
hello-world          latest              2cb0d9787c4d        5 weeks ago         1.85kB

Starting and stopping instances

Here is the way we stop the instances.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
462658ae9e5a        ubuntu              "bash"              15 hours ago        Up 15 hours                             inspiring_mahavira
$ docker stop 462658ae9e5a
462658ae9e5a

To start the instance the instance customized by us..

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
pandian/centos_ssh latest 7f019be4424c 43 minutes ago 294MB
centos latest 5182e96772bf 10 days ago 200MB
ubuntu latest 735f80812f90 3 weeks ago 83.5MB
hello-world latest 2cb0d9787c4d 5 weeks ago 1.85kB
$ docker run -it pandian/centos_ssh
[root@d66d18a06066 /]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 25 bytes 3024 (2.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0